Transactionモデルがあります。トランザクションにはseller_id列とがありbuyer_idます。どちらもUserIDで埋められます。
それで:
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
私がやりたいのは、売り手か買い手かに関係なく、不完全なトランザクションを関連付けるhas_manyためにを追加することです。UserUser
class User
has_many :incomplete_transactions, :class_name => 'Transaction', :conditions => ???
end
_
私はそれを純粋なSQLで書き、私が望む結果を得ました。私のSQLの結合は次のとおりです。
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))
joinそれをhas_manyアソシエーションに変換するにはどうすればよいですか?
編集:
incomplete_transactionsを(配列ではなく)ActiveRecord :: Relationshipとして保持したいと思っていたので、user.incomplete_transactions.limit(15)のようなことができます。
ありがとう。