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;
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
add a comment |
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
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
add a comment |
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
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
ruby-on-rails postgresql activerecord
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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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