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

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

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

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

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.