Vikrant Rathore
2006-Jan-13 03:51 UTC
[Rails] Single Table Inheritance (this is my 3rd post :( )
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 Olbrich
2006-Jan-13 04:46 UTC
[Rails] Re: Single Table Inheritance (this is my 3rd post :( )
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 -- Posted via http://www.ruby-forum.com/.
Justin Forder
2006-Jan-13 07:17 UTC
[Rails] Single Table Inheritance (this is my 3rd post :( )
Vikrant Rathore wrote:> 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 > endWithout 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 Lists
2006-Jan-13 16:22 UTC
[Rails] Re: Single Table Inheritance (this is my 3rd post :( )
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 Forder wrote:> Vikrant Rathore wrote: >> 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 > > 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
Justin Forder
2006-Jan-14 10:38 UTC
[Rails] Re: Single Table Inheritance (this is my 3rd post :( )
Mailing Lists wrote:> 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.In case it wasn''t clear, I was suggesting that you shouldn''t use inheritance - just use Company in multiple roles. Good luck! Justin