belongs_to the same table 2 times

Hi ~

I have the following database design

class Subscription < ActiveRecord::Base
  belongs_to :user, :class_name => "User", :foreign_key =>
'subscriber_user_id'
  belongs_to :user, :class_name => "User", :foreign_key =>
'supplier_user_id'
end

and there existing a user table. How can i access the subscriber and
supplier in the User object? Because user.subscription may has
ambiguous for accessing supplier and subscriber

Thank ~
Patrick

Patrick

You can use similar constraints on a has_many relationship. I think this should do it for you.

In

class User < AR::Base

has_many :subscribers, :class_name => “Subscription”, :foreign_key => “subscriber_user_id”

has_many :suppliers, :class_name => “Subscription”, :foreign_key => “supplier_user_id”

end

then

@user.subscribers

@user.suppliers

Thanks, that's work ~~~