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

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현