# What is the best way of setting up the following model:

**URL:** <https://www.ruby-forum.com/t/what-is-the-best-way-of-setting-up-the-following-model/51158>\
**Category:** Rails\
**Created:** [December 27, 2005, 11:50am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-of-setting-up-the-following-model/51158 "2005-12-27T11:50:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [December 27, 2005, 11:50am UTC](https://www.ruby-forum.com/t/what-is-the-best-way-of-setting-up-the-following-model/51158/1 "2005-12-27T11:50:54Z")

</div>

Hi, I am new to Rails (and Ruby) and for what i have seen sofar i like  
it a lot! But i can’t figure out what the best sollution would be for  
the following:

Lets say i want to setup a CRM system and i have to sorts of contacts:  
Customers and Prospects. A part of the info is the same (addresses,  
company info etc) for both customers an prospects and a part is  
different.

What i would normally do is defining tree tabels: organisations for the  
generic info, customerdetails for the customer specific info and  
prospectdetails for the prospect details.

Would you also do this in a Rails application?  
and if so, how would the models look like? would the customer and  
prospect model both use the same table ‘organisations’?  
is it hard to retrieve separate lists of customers and prospects?

or is the above way not the ‘rails way’?

i hope this makes any sense

Regards,

Remco

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [December 27, 2005, 12:38pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-of-setting-up-the-following-model/51158/2 "2005-12-27T12:38:05Z")

</div>

> What i would normally do is defining tree tabels: organisations for the  
> generic info, customerdetails for the customer specific info and  
> prospectdetails for the prospect details.

A minimal way of doing what you describe in Rails, using associations:

class Organization \< ActiveRecord::Base  
has\_many :customers  
has\_many :prospective\_customers  
end

class Customer \< ActiveRecord::Base  
belongs\_to :organization  
end

class ProspectiveCustomer \< ActiveRecord::Base  
belongs\_to :organization  
end

With this approach you have three models, and three tables. The  
‘customers’ and ‘prospective\_customers’ tables each have a field  
‘organization\_id’, which refers to a column in the ‘organizations’  
table.

Alternatively, you could use Single Table Inheritance (STI) [1]  
instead of associations. With STI, your single table would include a  
‘type’ column, which indicates the AR class of each row. So you’d have

class Organization \< ActiveRecord::Base  
end

class Customer \< Organization  
end

class ProspectiveCustomer \< Organization  
end

The ‘organizations’ table would have a ‘type’ field set to either  
‘Customer’ or ‘ProspectiveCustomer’.

I recommend you use STI only if the data model for Customer and  
ProspectiveCustomer is very similar. If they’re bound to get very  
specialized, use associations instead.

cheers  
Gerret

[1] [Peak Obsession](http://api.rubyonrails.com/classes/ActiveRecord/Base.html)

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [December 27, 2005, 1:01pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-of-setting-up-the-following-model/51158/3 "2005-12-27T13:01:18Z")

</div>

Gerret A. wrote:

> > What i would normally do is defining tree tabels: organisations for the  
> > generic info, customerdetails for the customer specific info and  
> > prospectdetails for the prospect details.
> 
> A minimal way of doing what you describe in Rails, using associations:
> 
> class Organization \< ActiveRecord::Base  
> has\_many :customers  
> has\_many :prospective\_customers  
> end

Hello Gerret,  
Thanks for your reply!  
One question: why do i have to use the has\_many statement?  
In my model, every organisation has only one record in the customer  
table or the prospective table. is it also possible to use the has\_one  
statement?

regards,

Remco

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [December 27, 2005, 1:33pm UTC](https://www.ruby-forum.com/t/what-is-the-best-way-of-setting-up-the-following-model/51158/4 "2005-12-27T13:33:09Z")

</div>

> In my model, every organisation has only one record in the customer  
> table or the prospective table. is it also possible to use the has\_one  
> statement?  
> Yes, you can use the ‘has\_one’ statement.
