title: Rails association with multiple foreign keys
body: <p>I want to be able to use two columns on one table to define a relationship. So using a task app as an example. </p>
  
  <p><strong>Attempt 1:</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :tasks
  end
  
  class Task &lt; ActiveRecord::Base
    belongs_to :owner, class_name: "User", foreign_key: "owner_id"
    belongs_to :assignee, class_name: "User", foreign_key: "assignee_id"
  end
  </code></pre>
  
  <p>So then <code>Task.create(owner_id:1, assignee_id: 2)</code></p>
  
  <p>This allows me to perform <code>Task.first.owner</code> which returns <em>user one</em> and <code>Task.first.assignee</code> which returns <em>user two</em> but <code>User.first.task</code> returns nothing. Which is because task doesn't belong to a user, they belong to <em>owner</em> and <em>assignee</em>. So,</p>
  
  <p><strong>Attempt 2:</strong></p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :tasks, foreign_key: [:owner_id, :assignee_id]
  end
  
  class Task &lt; ActiveRecord::Base
    belongs_to :user
  end
  </code></pre>
  
  <p>That just fails altogether as two foreign keys don't seem to be supported. </p>
  
  <p>So what I want is to be able to say <code>User.tasks</code> and get both the users owned and assigned tasks. </p>
  
  <p>Basically somehow build a relationship that would equal a query of <code>Task.where(owner_id || assignee_id == 1)</code></p>
  
  <p>Is that possible?</p>
  
  <h1>Update</h1>
  
  <p>I'm not looking to use <code>finder_sql</code>, but this issue's unaccepted answer looks to be close to what I want: <a href="https://stackoverflow.com/questions/3421873/rails-multiple-index-key-association">Rails - Multiple Index Key Association</a></p>
  
  <p>So this method would look like this,</p>
  
  <p><strong>Attempt 3:</strong></p>
  
  <pre><code>class Task &lt; ActiveRecord::Base
    def self.by_person(person)
      where("assignee_id =&gt; :person_id OR owner_id =&gt; :person_id", :person_id =&gt; person.id
    end 
  end
  
  class Person &lt; ActiveRecord::Base
  
    def tasks
      Task.by_person(self)
    end 
  end
  </code></pre>
  
  <p>Though I can get it to work in <code>Rails 4</code>, I keep getting the following error:</p>
  
  <pre><code>ActiveRecord::PreparedStatementInvalid: missing value for :owner_id in :donor_id =&gt; :person_id OR assignee_id =&gt; :person_id
  </code></pre>