Forum: Ruby on Rails Single Table Inheritance (this is my 3rd post :( )

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Vikrant Rathore (Guest)
on 2006-01-13 05:52
(Received via mailing list)
Hi Everyone,

I hope I get some feedback on my question. Here it goes.

I have a situation here, I have a company table and then using Single
Table inheritance it is of 3 types:

class Company < ActiveRecord::Base
end

class Vendor < Company
end

class Customer < Company
end

class Agent < Company
end

Now I have a active record Association

class QuoteSheet < ActiveRecord::Base
belongs_to :customer
has_many :vendors
has_one :Agent
end

Now in the table quote_sheets should I use vendor_id, customer_id,
agent_id or just company_id since all of them are in 1 table called
companies.

I have read agile wweb development with rail, and no where it says about
relationship like this.

Thanks
with best regards,
Vikrant
Kevin O. (Guest)
on 2006-01-13 06:46
Vikrant Rathore wrote:
> class QuoteSheet < ActiveRecord::Base
> belongs_to :customer
> has_many :vendors
> has_one :Agent
> end
>
> Now in the table quote_sheets should I use vendor_id, customer_id,
> agent_id or just company_id since all of them are in 1 table called
> companies.
>

Yeah, it is a bit unclear, but this is what I would try...

class Quotesheet
  belongs_to :customer, :foreign_key=>'customer_id'

...

class Customer
  has_many :quote_sheets, :foreign_key +> 'customer_id'
...

Since you are using belongs_to/has_many I assume you don't really want a
HABTM, which is the only reason you would need the 'quote_sheets' table.

_Kevin
Justin F. (Guest)
on 2006-01-13 09:17
(Received via mailing list)
Vikrant Rathore wrote:
> class Vendor < Company
> class QuoteSheet < ActiveRecord::Base
> belongs_to :customer
> has_many :vendors
> has_one :Agent
> end

Without knowing all the details I can't be sure, but my first impression
is that vendor, customer and agent are roles rather than types. Could
the same Company be a vendor with respect to one QuoteSheet, and a
customer with respect to another? If you use subclasses in the way you
suggest, this wouldn't be possible.

I think your associations need a little attention:

belongs_to :customer looks fine - so a customer would be able to have
many QuoteSheets

has_many :vendors looks wrong - that would mean a vendor would belong to
  one QuoteSheet - I think it should be has_and_belongs_to_many

has_one :agent (not :Agent) looks wrong - that would mean an agent would
belong to one QuoteSheet - it should be belongs_to (which really means
refers_to)

QuoteSheet might look something like:

class QuoteSheet < ActiveRecord::Base

   belongs_to :customer,
              :class_name => "Company",
              :foreign_key => "customer_id"

   belongs_to :agent,
              :class_name => "Company",
              :foreign_key => "agent_id"

   has_and_belongs_to_many :vendors,
              :class_name => "Company",
              :join_table => "quote_sheets_vendors"

end

(absolutely untested!)

You would have to think about how to name the other ends of the
associations, i.e. the collection of QuoteSheets received as a customer,
the collection of QuoteSheets issued as an agent, and the collection of
QuoteSheets quoted on as a vendor.

Is there a bit of model missing here - the actual quote from each
vendor?

HTH

   Justin
Mailing L. (Guest)
on 2006-01-13 18:25
(Received via mailing list)
Hi Justin and Kevin,

Thanks for your response. This is just an illustration of the problem
indeed the actual DB design includes over 50 tables and different since
this application is for a large Supply Chain Management (in
commodities). Currently we have an .net based system with 150 tables and
another 2 systems based on J2EE with over 200 tables. I was able to
squeeze all these into little over 50 tables.

Now I want to build a solution on one top of the other. But then stuck
at the single inheritance problem. But it seems your suggested method
might work in which i specify the foreign key.

Thanks
with best regards
Vikrant
Justin F. (Guest)
on 2006-01-14 12:40
(Received via mailing list)
Mailing L. wrote:
> at the single inheritance problem. But it seems your suggested method
> might work in which i specify the foreign key.

In case it wasn't clear, I was suggesting that you shouldn't use
inheritance - just use Company in multiple roles.

Good luck!

   Justin
This topic is locked and can not be replied to.