Patrick4
(Patrick)
August 17, 2007, 6:01am
1
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
Daniel_N
(Daniel N)
August 17, 2007, 6:11am
2
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