Hi there, I am newbie on RoR and I am wondering how to solve the following situation using "has_x" and "belongs_to" (and eventually polymorphism if needed). Let say that I have a Salesman table containing salesmen :) Let say that I have a Customer table containing customers information _and_ a prefered Salesman (salesman_id) Let say that I have an Order table, orders are linked to customers (the order "belongs to" a customer) and to salesmen (the order have been managed by a particular salesman (salesman_id) which is not necessary the customer prefered salesman). Could someone explain me how to declare models and tables (keys) so that those relationships are working ? Many thanks for your help! Georges --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
class Salesman > ActiveRecord::Base has_many :primary_customers, :class_name => ''Customer'', :foreign_key => ''preferred_salesman_id'' has_many :orders has_many :customers, :through => :orders end Table name: salesmen Columns: id class Customer > ActiveRecord::Base has_many :orders belongs_to :preferred_salesman, :class_name => ''Salesman'', :foreign_key => ''preferred_salesman_id'' has_many :salesmen, :through => :orders end Table name: customers Columns: id, perferred_salesman_id class Order > ActiveRecord::Base belongs_to :salesman belongs_to :customer end Table name: orders columns: id, salesman_id, customer_id c = Customer.find(1) c.salesmen # => array of all salesmen affiliated with any order c.order[0].salesman # => salesman affiliated with a specific order c.preferred_salesman # => the guy that cuts the best deals s = Salesman.find(1) s.primary_customers # => all customers for whom the salesman has completely pulled the wool over their eyes s.customers # => all customers affliated with any order s.orders[0].customer # => customer associated with a specific order Regards, Ryan On Sep 8, 6:01 am, "Georges A." <garnould.spam...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi there, > > I am newbie on RoR and I am wondering how to solve the following > situation using "has_x" and "belongs_to" (and eventually polymorphism > if needed). > > Let say that I have a Salesman table containing salesmen :) > > Let say that I have a Customer table containing customers > information _and_ a prefered Salesman (salesman_id) > > Let say that I have an Order table, orders are linked to customers > (the order "belongs to" a customer) and to salesmen (the order have > been managed by a particular salesman (salesman_id) which is not > necessary the customer prefered salesman). > > Could someone explain me how to declare models and tables (keys) so > that those relationships are working ? > > Many thanks for your help! > > Georges--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ryan, that''s a great help! Thanks! Regards, Georges On Sep 8, 9:03 pm, Ryan <Ryan.Warner...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Salesman > ActiveRecord::Base > has_many :primary_customers, :class_name => ''Customer'', :foreign_key > => ''preferred_salesman_id'' > has_many :orders > has_many :customers, :through => :orders > end > > Table name: salesmen > Columns: id > > class Customer > ActiveRecord::Base > has_many :orders > belongs_to :preferred_salesman, :class_name => > ''Salesman'', :foreign_key => ''preferred_salesman_id'' > has_many :salesmen, :through => :orders > end > > Table name: customers > Columns: id, perferred_salesman_id > > class Order > ActiveRecord::Base > belongs_to :salesman > belongs_to :customer > end > > Table name: orders > columns: id, salesman_id, customer_id > > c = Customer.find(1) > c.salesmen # => array of all salesmen affiliated with any order > c.order[0].salesman # => salesman affiliated with a specific order > c.preferred_salesman # => the guy that cuts the best deals > > s = Salesman.find(1) > s.primary_customers # => all customers for whom the salesman has > completely pulled the wool over their eyes > s.customers # => all customers affliated with any order > s.orders[0].customer # => customer associated with a specific order > > Regards, > Ryan > > On Sep 8, 6:01 am, "Georges A." <garnould.spam...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi there, > > > I am newbie on RoR and I am wondering how to solve the following > > situation using "has_x" and "belongs_to" (and eventually polymorphism > > if needed). > > > Let say that I have a Salesman table containing salesmen :) > > > Let say that I have a Customer table containing customers > > information _and_ a prefered Salesman (salesman_id) > > > Let say that I have an Order table, orders are linked to customers > > (the order "belongs to" a customer) and to salesmen (the order have > > been managed by a particular salesman (salesman_id) which is not > > necessary the customer prefered salesman). > > > Could someone explain me how to declare models and tables (keys) so > > that those relationships are working ? > > > Many thanks for your help! > > > Georges--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---