class Foo < ActiveRecord::Base {
has_many :bars
}
foo = Foo.find(1)
foo.bars.count
Is this the best way to count the number of bars or is there an efficient way
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your model is completely normalized then this will be the most efficent way.
If you need to do this a lot, you can cache the
barscount on theFootable.Railscast Episode 23 covers counter cache, this information is still correct an relevant in the current Rails version.