Say I have 2 models:
class Person < ActiveRecord::Base
has_many :addresses
end
class Address < ActiveRecord::Base
belongs_to :person
end
I want to get all people who have exactly 2 addresses. Is there an easy ActiveRecord/Arel way of doing this with a subquery? I don’t want to use a counter_cache to do it.
This worked for me (using Rails 3, PostGreSQL):
In your case, the following should return the Persons with exactly 2 Addresses: