Topic: Two Foreign Keys within one table, referencing the same table

I have a properties table that references the users table. So far, no problem. However, there are two types of users that can have a relationship to each property: an owner and an agent (in charge of the sale). At present, I have a setup like this:

    properties:
    agent_id
    owner_id

So the Property class looks like this:
    class Property < ActiveRecord::Base
      belongs_to :user, :foreign_key => "agent_id"
      belongs_to :user, :foreign_key => "owner_id"
      ...
    end

And the User class:

    class User < ActiveRecord::Base
      has_many :properties
      ...
    end

I want to be able to use the method
  property.agent

But my current setup doesn't activate this method, so I have to establish the link by hand. Is there a way to enable the has_many relationship when the FK is a duplicate of another, pointing to the same table?

Re: Two Foreign Keys within one table, referencing the same table

You're really close!

class Property < ActiveRecord::Base
  belongs_to :agent, :foreign_key => "agent_id", :class_name => :user
  belongs_to :owner, :foreign_key => "owner_id", :class_name => :user
end

Re: Two Foreign Keys within one table, referencing the same table

Thanks, you gave me that answer almost exactly at the same time as Ray from sixliteral.com

Very neat :-)