Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Fixed it!</p> <p>This is very similar to @DRobinson's solution, but using a proc and a has_many definition instead of defining a local method.</p> <p>According to the 3.1 release notes you can now use a proc in a condition!</p> <p>Inside the proc, self is the object which is the owner of the association, unless you are eager loading the association, in which case self is the class which the association is within.</p> <pre><code>class User &lt; ActiveRecord::Base has_many :incomplete_transactions , :class_name =&gt; 'Transaction', :conditions =&gt; proc { incomplete_sales + incomplete_purchases } 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 } 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' end class Transaction &lt; ActiveRecord::Base belongs_to :seller, :class_name =&gt; 'User' belongs_to :buyer, :class_name =&gt; 'User' # scope :incomplete_sales , :conditions =&gt; {:buyer_id =&gt; nil} # scope :incomplete_purchases , :conditions =&gt; {:seller_id =&gt; nil} end </code></pre> <p>See: </p> <ul> <li><a href="http://guides.rubyonrails.org/3_1_release_notes.html" rel="nofollow">http://guides.rubyonrails.org/3_1_release_notes.html</a></li> </ul>
 

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