Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Similar answer to <a href="https://stackoverflow.com/a/11872798/624590">pdevisser's</a>: You could use something similar to the answer given on this question: <a href="https://stackoverflow.com/a/307724/624590">https://stackoverflow.com/a/307724/624590</a></p> <p>Which would basically result in:</p> <pre><code>class User has_many :selling_transactions, :class_name =&gt; 'Transaction', :foreign_key =&gt; 'seller_id' has_many :buying_transactions, :class_name =&gt; 'Transaction', :foreign_key =&gt; 'buyer_id' has_many :incomplete_sales, :class_name =&gt; 'Transaction', :foreign_key =&gt; 'seller_id', :conditions =&gt; { :buyer_id =&gt; nil } has_many :incomplete_purchases, :class_name =&gt; 'Transaction', :foreign_key =&gt; 'buyer_id', :conditions =&gt; { :seller_id =&gt; nil } def incomplete_transactions incomplete_sales + incomplete_purchases end end </code></pre> <p><strong>EDIT</strong>: Alright, not totally sure how to set it up the way you wish (using has_many), but something along these lines might work for you:</p> <pre><code>class User has_many :selling_transactions, :class_name =&gt; 'Transaction', :foreign_key =&gt; 'seller_id' has_many :buying_transactions, :class_name =&gt; 'Transaction', :foreign_key =&gt; 'buyer_id' def incomplete_transactions Transaction.where("(buyer_id = ? and seller_id = NULL) or (seller_id = ? and buyer_id = NULL)", id, id) end end </code></pre> <p>The where statement will return an <code>ActiveRecord::Association</code>, so you can follow it with limit (or other activerecord functions) when calling it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload