title: Rails - two foreign keys on one model both refer to same model
body: <p>I'm fairly new to ActiveRecord associations. I'm sketching out an application that tracks who owes each other money among a set of users. An <code>Expense</code> model and a <code>User</code> model seem like natural choices, I'm just not sure how to define the relationship between the two. For example, I want to track the creditor ("owner") and the debtor of each expense, but that's really just two foreign keys that go back to User. In addition, each user can have multiple expenses (both as creditor and debtor) My best guess for the associations thus far is something like:</p>
  
  <pre><code>class Expense
      # belongs_to or has_one here?
      # Not sure about class =&gt; User syntax:
      # need to alias to foreign keys that reference the same model
      belongs_to :creditor, :class =&gt; User 
      belongs_to :debtor, :class =&gt; User
  
  class User
      # has_many expenses defines a creditor relationship (user owns expense)
      # how to define debtor relationship? (belongs_to...?)
      has_and_belongs_to_many :expenses
  </code></pre>
  
  <p>I've read the Rails guide on associations but I'm still fairly lost on foreign keys and join tables. Any input is much appreciated!</p>