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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 8/17/07, Patrick <patricksky852-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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 ~ > PatrickPatrick 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, that''s work ~~~ On Aug 17, 2:11 pm, "Daniel N" <has....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8/17/07, Patrick <patricksky...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---