rails get total count in has many relationA concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?rails use counts in different viewsRuby on Rails Server optionsSyncing iphone contacts with RailsHow to use concerns in Rails 4Getting error: Peer authentication failed for user “postgres”, when trying to get pgsql working with railsPreloading of chain of complex/indirect ActiveRecord functions/'associations'

What reasons are there for a Capitalist to oppose a 100% inheritance tax?

Where does SFDX store details about scratch orgs?

Facing a paradox: Earnshaw's theorem in one dimension

What is the intuition behind short exact sequences of groups; in particular, what is the intuition behind group extensions?

Anagram holiday

Twin primes whose sum is a cube

Could gravitational lensing be used to protect a spaceship from a laser?

Can a virus destroy the BIOS of a modern computer?

Is it canonical bit space?

How do conventional missiles fly?

Why is the 'in' operator throwing an error with a string literal instead of logging false?

How can saying a song's name be a copyright violation?

Why can't we play rap on piano?

Is "remove commented out code" correct English?

Why doesn't H₄O²⁺ exist?

Were any external disk drives stacked vertically?

Combinations of multiple lists

What exploit are these user agents trying to use?

What about the virus in 12 Monkeys?

Is it possible to create light that imparts a greater proportion of its energy as momentum rather than heat?

Intersection of two sorted vectors in C++

Western buddy movie with a supernatural twist where a woman turns into an eagle at the end

Memorizing the Keyboard

Is it inappropriate for a student to attend their mentor's dissertation defense?



rails get total count in has many relation


A concise explanation of nil v. empty v. blank in Ruby on RailsUnderstanding the Rails Authenticity TokenHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?rails use counts in different viewsRuby on Rails Server optionsSyncing iphone contacts with RailsHow to use concerns in Rails 4Getting error: Peer authentication failed for user “postgres”, when trying to get pgsql working with railsPreloading of chain of complex/indirect ActiveRecord functions/'associations'






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm attempting to do some grouping with a custom select count. Here are my models



class RescueGroup < ApplicationRecord
has_many :addresses, dependent: :delete_all

has_many :emails, -> where(type: Contact.types[:email]) , class_name: 'Contact'
has_many :phones, -> where(type: Contact.types[:phone]) , class_name: 'Contact'
end

class Contact < ApplicationRecord
self.inheritance_column = nil

enum type: [:email, :phone, :facebook]

belongs_to :rescue_group
end

class Address < ApplicationRecord
belongs_to :rescue_group
end


I'm attempting to build a scope that will allow me to get a count of all emails for each rescue group so I can show the count in a view.



But if I do this it works and I can get the email_count field.



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group(:id)
.first
.email_count


But as soon as I add a filter like this:



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group('rescue_groups.id, addresses.id')
.where.not(addresses: state: 'tas' )
.first
.email_count


It will fail with the following errror



NoMethodError: undefined method `email_count'


It will also fail if I use the first query and attempt to pluck email_count:



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group(:id)
.pluck(:email_count)


So my question is how can I get the email_count from both of the failed queries above?










share|improve this question
























  • Why can't you just do: rg = RescueGroup.find(x); rg.emails.count ?

    – Msencenb
    Mar 22 at 2:20












  • hey @Msencenb, I need to get the results for hundreds of thousands of records, not an individual one.

    – kurupt_89
    Mar 22 at 4:00

















0















I'm attempting to do some grouping with a custom select count. Here are my models



class RescueGroup < ApplicationRecord
has_many :addresses, dependent: :delete_all

has_many :emails, -> where(type: Contact.types[:email]) , class_name: 'Contact'
has_many :phones, -> where(type: Contact.types[:phone]) , class_name: 'Contact'
end

class Contact < ApplicationRecord
self.inheritance_column = nil

enum type: [:email, :phone, :facebook]

belongs_to :rescue_group
end

class Address < ApplicationRecord
belongs_to :rescue_group
end


I'm attempting to build a scope that will allow me to get a count of all emails for each rescue group so I can show the count in a view.



But if I do this it works and I can get the email_count field.



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group(:id)
.first
.email_count


But as soon as I add a filter like this:



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group('rescue_groups.id, addresses.id')
.where.not(addresses: state: 'tas' )
.first
.email_count


It will fail with the following errror



NoMethodError: undefined method `email_count'


It will also fail if I use the first query and attempt to pluck email_count:



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group(:id)
.pluck(:email_count)


So my question is how can I get the email_count from both of the failed queries above?










share|improve this question
























  • Why can't you just do: rg = RescueGroup.find(x); rg.emails.count ?

    – Msencenb
    Mar 22 at 2:20












  • hey @Msencenb, I need to get the results for hundreds of thousands of records, not an individual one.

    – kurupt_89
    Mar 22 at 4:00













0












0








0








I'm attempting to do some grouping with a custom select count. Here are my models



class RescueGroup < ApplicationRecord
has_many :addresses, dependent: :delete_all

has_many :emails, -> where(type: Contact.types[:email]) , class_name: 'Contact'
has_many :phones, -> where(type: Contact.types[:phone]) , class_name: 'Contact'
end

class Contact < ApplicationRecord
self.inheritance_column = nil

enum type: [:email, :phone, :facebook]

belongs_to :rescue_group
end

class Address < ApplicationRecord
belongs_to :rescue_group
end


I'm attempting to build a scope that will allow me to get a count of all emails for each rescue group so I can show the count in a view.



But if I do this it works and I can get the email_count field.



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group(:id)
.first
.email_count


But as soon as I add a filter like this:



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group('rescue_groups.id, addresses.id')
.where.not(addresses: state: 'tas' )
.first
.email_count


It will fail with the following errror



NoMethodError: undefined method `email_count'


It will also fail if I use the first query and attempt to pluck email_count:



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group(:id)
.pluck(:email_count)


So my question is how can I get the email_count from both of the failed queries above?










share|improve this question
















I'm attempting to do some grouping with a custom select count. Here are my models



class RescueGroup < ApplicationRecord
has_many :addresses, dependent: :delete_all

has_many :emails, -> where(type: Contact.types[:email]) , class_name: 'Contact'
has_many :phones, -> where(type: Contact.types[:phone]) , class_name: 'Contact'
end

class Contact < ApplicationRecord
self.inheritance_column = nil

enum type: [:email, :phone, :facebook]

belongs_to :rescue_group
end

class Address < ApplicationRecord
belongs_to :rescue_group
end


I'm attempting to build a scope that will allow me to get a count of all emails for each rescue group so I can show the count in a view.



But if I do this it works and I can get the email_count field.



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group(:id)
.first
.email_count


But as soon as I add a filter like this:



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group('rescue_groups.id, addresses.id')
.where.not(addresses: state: 'tas' )
.first
.email_count


It will fail with the following errror



NoMethodError: undefined method `email_count'


It will also fail if I use the first query and attempt to pluck email_count:



RescueGroup.select('rescue_groups.*, count(contacts.id) FILTER(where type = 0) as email_count')
.joins(:emails)
.includes(:addresses)
.group(:id)
.pluck(:email_count)


So my question is how can I get the email_count from both of the failed queries above?







ruby-on-rails postgresql activerecord






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 22:35







kurupt_89

















asked Mar 21 at 21:50









kurupt_89kurupt_89

78652558




78652558












  • Why can't you just do: rg = RescueGroup.find(x); rg.emails.count ?

    – Msencenb
    Mar 22 at 2:20












  • hey @Msencenb, I need to get the results for hundreds of thousands of records, not an individual one.

    – kurupt_89
    Mar 22 at 4:00

















  • Why can't you just do: rg = RescueGroup.find(x); rg.emails.count ?

    – Msencenb
    Mar 22 at 2:20












  • hey @Msencenb, I need to get the results for hundreds of thousands of records, not an individual one.

    – kurupt_89
    Mar 22 at 4:00
















Why can't you just do: rg = RescueGroup.find(x); rg.emails.count ?

– Msencenb
Mar 22 at 2:20






Why can't you just do: rg = RescueGroup.find(x); rg.emails.count ?

– Msencenb
Mar 22 at 2:20














hey @Msencenb, I need to get the results for hundreds of thousands of records, not an individual one.

– kurupt_89
Mar 22 at 4:00





hey @Msencenb, I need to get the results for hundreds of thousands of records, not an individual one.

– kurupt_89
Mar 22 at 4:00












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55289785%2frails-get-total-count-in-has-many-relation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55289785%2frails-get-total-count-in-has-many-relation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript