Topic: 1 Model with Multiple foriegn keys to another model.

Im writing an app that has a user model, and the users in this model can be two different user types.


class User < ActiveRecord::Base
  has_many        :transactions
  has_many        :transaction_details, :through => :transactions
  has_many        :renters, :class_name => "Transactions"


class Transaction < ActiveRecord::Base
  has_many        :transaction_details
  belongs_to      :user
  belongs_to      :renter, :class_name => "User"


class TransactionDetail < ActiveRecord::Base
  belongs_to :transaction
  belongs_to :inventory
  scope :all_open, where("transaction_details.checked_in_at is null")
  scope :all_closed, where("transaction_details.checked_in_at is not null")


Basically a user could be a renter, or the person checking the item out.  Once I have a transaction, I can call:

@transaction.renter.first_name  # receive the first name of the renter from the renter table
@transaction.user.first_name     # receive the first name of user who performed the transaction

This is perfect and works as I explected.  For the life of me, I can not figure out how to get the scope to work when called through a user:

    # trying to perform the scrope "all_open", limted to the current user record, but I cant get it to use the 
    # renter_id column instead of the user_id column

is this possible to have a scrope look up by the second forien_key instead of user_id?


Last edited by todd.sanders79 (2012-08-06 01:21:43)