# Multiple kind of relationships, Howto?

**URL:** <https://www.ruby-forum.com/t/multiple-kind-of-relationships-howto/114139>\
**Category:** Rails\
**Created:** [September 8, 2007, 1:02pm UTC](https://www.ruby-forum.com/t/multiple-kind-of-relationships-howto/114139 "2007-09-08T13:02:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Georges\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ea924/32.png) [@Georges\_A](https://www.ruby-forum.com/u/Georges_A)\
**Post date:** [September 8, 2007, 1:02pm UTC](https://www.ruby-forum.com/t/multiple-kind-of-relationships-howto/114139/1 "2007-09-08T13:02:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Georges\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ea924/32.png) [@Georges\_A](https://www.ruby-forum.com/u/Georges_A)\
**Post date:** [September 8, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/multiple-kind-of-relationships-howto/114139/2 "2007-09-08T21:04:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Georges\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/7ea924/32.png) [@Georges\_A](https://www.ruby-forum.com/u/Georges_A)\
**Post date:** [September 10, 2007, 3:07pm UTC](https://www.ruby-forum.com/t/multiple-kind-of-relationships-howto/114139/3 "2007-09-10T15:07:07Z")

</div>

Ryan, that’s a great help! Thanks!

Regards,

Georges
