If I have a model called User that has:
has_many :parent_relationships,
:class_name => "Relationship",
:foreign_key => :child_id,
:dependent => :destroy,
:uniq => true
has_many :parents,
:through => :parent_relationships,
:source => :parent
So, I want to be able to find the Users who have at least one parent. Right now, if I fetch a particular user and I do user.parents, I get the list of parents, but how would I do that in a where clause?
My concern is that right now I am fetching like this:
User.where({:role => 'Teen'})
So, I want to find all the Users whose role is a Teen and who at least have a parent. Also, and to make it more complicated, I have another association that I would like to take into account:
has_and_belongs_to_many :notifications
And I would like to find those Users who has a notification which name attribute is “email”.
Thanks
A join should do it:
Since this creates an
INNER JOIN, it will only return the records with parents. Theuniqremoves duplicate records that may be returned by the join.EDIT:
Your method chaining could work something like this: