1

I have a User table which

has_many :meetings, through: registrations

I would like to do an ActiveRecord query for something like this

User.where("meetings > 0")

But obviously there is no "meetings" column in my db. Which model should I add a class method on and what would that method look like? Thanks for your help.

Kormie
  • 127
  • 7

1 Answers1

0

Have a look at using a "counter cache" on your association:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

Pavling
  • 3,933
  • 1
  • 22
  • 25
  • the api docs make it seem like this only works in a belongs_to relationship – Kormie Nov 15 '11 at 22:01
  • If Foo has_many Bars, then the foos table gets a bars_count field to store the cache of the count, that it's specified on the belongs_to is just the syntax – Pavling Nov 15 '11 at 22:27
  • PS Googling for "rails counter cache has_many through" gleans this: http://stackoverflow.com/questions/5256897/counter-cache-with-has-many-through – Pavling Nov 15 '11 at 22:34