In other words, each Member is in a Group, and has a Role in this
Group(the triples member-group-role are defined in the model
Membership). On the other hand, each Message is associated to a
Membership. If we look at the two sets:
1. All Messages with is_pending=true
2. All the Memberships where Member m has role "admin"
... then I need the messages from (1.), which belong to the Memberships
from (2.)
I realize it's quite complicated, but if you can suggest me another way
to achieve this, I'll be happy to change my approach.
One way to do this is to join the tables which have the extra
information so that you can then put conditions on those. So at a
basic level if it was just messages and memberships the raw sql would
be something like
select messages.* from messages
inner join memberships on memberships.id = messages.membership_id
where some conditions on messages and memberships.
You don't need to write out all this, Message.find :all, :joins
=> :memberships, :conditions => [...]
You can join several layers down - there's some examples at