I have a Transaction
model. A transaction has a seller_id
column and a buyer_id
. Both are filled with a User
ID.
so:
class Transaction
belongs_to :seller, :class_name => 'User'
belongs_to :buyer, :class_name => 'User'
end
_
class User
has_many :selling_transactions, :class_name => 'Transaction', :foreign_key => 'seller_id'
has_many :buying_transactions, :class_name => 'Transaction', :foreign_key => 'buyer_id'
end
What I want to do is add a has_many
to User
to associate transactions that are incomplete, whether the User
is the seller or the buyer.
class User
has_many :incomplete_transactions, :class_name => 'Transaction', :conditions => ???
end
_
I wrote it out in pure SQL, and got the results I wanted. The join in my SQL is:
left outer join transactions t on ((t.seller_id = users.id and t.buyer_id is NULL) or (t.buyer_id = users.id and t.seller_id is NULL))
How do I translate that join
to a has_many
association?
EDIT:
I was hoping to keep incomplete_transactions as a ActiveRecord::Relation (instead of an Array), so I can do something like user.incomplete_transactions.limit(15)
Thanks.