In a nutshell, when I create a Transaction
Record it has two foreign keys. for the two users that participate in a Transaction
, ie:
Now, What I would like to know with your kind help, is How do I establish the relationship(s) between: User
and Transaction
Models, so that I can easily retrieve ALL the Transaction
s for either of the two User
s.
Something like:
user_one = User.find(1)
user_two = User.find(2)
user_one.transactions # returns all Transactions where user_one.id ==
# user_one_id Or user_one.id == user_two_id
user_two.transactions # returns all Transactions where user_two.id ==
# user_one_id Or user_two.id == user_two_id
What's the best way to achieve this? Is it best to establish foreign keys in the Transaction Model in this case? Or is this a problem to be solved via ActiveRecordQuery
only?
Thanks in advance.
If you have two user ids and want to query
Transaction
on some combination of them, you can use anor
clause: