HighDots Forums  

table inheritance in rails

Ruby On Rails Talk Ruby On Rails programming language mailing list


Discuss table inheritance in rails in the Ruby On Rails Talk forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Aljaz Fajmut
 
Posts: n/a

Default table inheritance in rails - 11-05-2009 , 07:23 PM






Hello,

Suppose I have 2 models Human and Person and I want to inherit one from
another so that I could use 'human' model/migration as a generic builder
for smiliar objects as 'person', for instance this with the following
table structure:

human table:
id
weight
size

person table:
id
name
surname
address

Is it possible to make such table inheritance in rails to be able to
have 'base' model for those that are submodels with different columns?

Thanks in advance!
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #2  
Old   
Conrad Taylor
 
Posts: n/a

Default Re: table inheritance in rails - 11-05-2009 , 08:05 PM






On Thu, Nov 5, 2009 at 4:23 PM, Aljaz Fajmut <
rails-mailing-list (AT) andreas-s (DOT) net> wrote:

Quote:
Hello,

Suppose I have 2 models Human and Person and I want to inherit one from
another so that I could use 'human' model/migration as a generic builder
for smiliar objects as 'person', for instance this with the following
table structure:

human table:
id
weight
size

person table:
id
name
surname
address

Is it possible to make such table inheritance in rails to be able to
have 'base' model for those that are submodels with different columns?

Thanks in advance!

Yes, this is possible in Rails. Thus, you can read section 19.4 of AWDwR
3rd edition. It
provides an excellent example of what one would do to achieve table
inheritance in Rails.

Good luck,

-Conrad


Quote:
--
Posted via http://www.ruby-forum.com/.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #3  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: table inheritance in rails - 11-05-2009 , 08:13 PM



Aljaz Fajmut wrote:
Quote:
Hello,

Suppose I have 2 models Human and Person and I want to inherit one from
another so that I could use 'human' model/migration as a generic builder
for smiliar objects as 'person',
Your data modeling is faulty. Human is not a sub- or superclass of
Person; rather, they're near synonyms.

Quote:
for instance this with the following
table structure:

human table:
id
weight
size

person table:
id
name
surname
address
Where's the inheritance here? There are no fields in common except id.
What do you actually want to do?

Quote:
Is it possible to make such table inheritance in rails to be able to
have 'base' model for those that are submodels with different columns?

Well, there's single-table inheritance (see the Rails docs), but it's
seldom a good idea...

Quote:
Thanks in advance!
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #4  
Old   
Aljaz Fajmut
 
Posts: n/a

Default Re: table inheritance in rails - 11-05-2009 , 08:44 PM



It might not be the best example but I couldnt think of better one in
the moment in wrote this. I have a clear image of what I want to
implement.

Is this single or multi-table inheritance?

Thanks for help

Marnen Laibow-Koser wrote:
Quote:
Aljaz Fajmut wrote:
Hello,

Suppose I have 2 models Human and Person and I want to inherit one from
another so that I could use 'human' model/migration as a generic builder
for smiliar objects as 'person',

Your data modeling is faulty. Human is not a sub- or superclass of
Person; rather, they're near synonyms.

for instance this with the following
table structure:

human table:
id
weight
size

person table:
id
name
surname
address

Where's the inheritance here? There are no fields in common except id.
What do you actually want to do?


Is it possible to make such table inheritance in rails to be able to
have 'base' model for those that are submodels with different columns?


Well, there's single-table inheritance (see the Rails docs), but it's
seldom a good idea...

Thanks in advance!

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #5  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: table inheritance in rails - 11-05-2009 , 09:07 PM



Aljaz Fajmut wrote:
Quote:
It might not be the best example but I couldnt think of better one in
the moment in wrote this. I have a clear image of what I want to
implement.
Then you should be able to explain it more clearly.

Quote:
Is this single or multi-table inheritance?
They're functionally more or less equivalent, although single-table
inheritance is rather a perversion of the relational model. The choice
between the two usually seems to be made on practical grounds.
Quote:
Thanks for help


Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #6  
Old   
Aljaz Fajmut
 
Posts: n/a

Default Re: table inheritance in rails - 11-05-2009 , 09:38 PM



Ok - I might be complicating too much.

Heres how I would do it with c++, not 100% sure what approach should I
take with rails on the database/model level.

If I want to have generic data-storage model Vehicle (which will hold
information the same information for all the vehicles - car, boat, etc.)

class vehicle holds:
color, max-speed, ..

class car:
color, max-speed, number of seats, ..

class boat:
color, max-speed, ... with some boat-specific columns

I'd just derive car from vehicle and use car-specific data columns
because similar operations will be called on it as on boat model.

As far as I understand RoR approach: if I have a lot of duplicated code
in my application, there must be some better solution for my problem.

What do you think?


Marnen Laibow-Koser wrote:
Quote:
Aljaz Fajmut wrote:
It might not be the best example but I couldnt think of better one in
the moment in wrote this. I have a clear image of what I want to
implement.

Then you should be able to explain it more clearly.


Is this single or multi-table inheritance?

They're functionally more or less equivalent, although single-table
inheritance is rather a perversion of the relational model. The choice
between the two usually seems to be made on practical grounds.

Thanks for help



Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #7  
Old   
Conrad Taylor
 
Posts: n/a

Default Re: table inheritance in rails - 11-05-2009 , 10:00 PM



On Thu, Nov 5, 2009 at 5:44 PM, Aljaz Fajmut <
rails-mailing-list (AT) andreas-s (DOT) net> wrote:

Quote:
It might not be the best example but I couldnt think of better one in
the moment in wrote this. I have a clear image of what I want to
implement.

Is this single or multi-table inheritance?


You can implement this as single table inheritance because a RDBMS
doesn't support multi-table inheritance (i.e.one cannot inherit the fields
of another table). If this is what you're looking for, then you'll need an
OODB which represents things as object graphs instead of table rows.
Lastly, Marnen is correct in saying that Person and Human are synonyms
of one another and I recommend reading up on object oriented design.

Good luck,

-Conrad


Quote:
Thanks for help

Marnen Laibow-Koser wrote:
Aljaz Fajmut wrote:
Hello,

Suppose I have 2 models Human and Person and I want to inherit one from
another so that I could use 'human' model/migration as a generic builder
for smiliar objects as 'person',

Your data modeling is faulty. Human is not a sub- or superclass of
Person; rather, they're near synonyms.

for instance this with the following
table structure:

human table:
id
weight
size

person table:
id
name
surname
address

Where's the inheritance here? There are no fields in common except id.
What do you actually want to do?


Is it possible to make such table inheritance in rails to be able to
have 'base' model for those that are submodels with different columns?


Well, there's single-table inheritance (see the Rails docs), but it's
seldom a good idea...

Thanks in advance!

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org

--
Posted via http://www.ruby-forum.com/.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #8  
Old   
Todd A. Jacobs
 
Posts: n/a

Default Re: table inheritance in rails - 11-07-2009 , 04:38 PM



On Fri, Nov 06, 2009 at 01:23:25AM +0100, Aljaz Fajmut wrote:


Quote:
human table: id weight size
person table: id name surname address
If I understand your question correctly, you need to build the right
relationships. If you lived in the Star Trek universe, you might have:

class Person < ActiveRecord::Base
belongs_to :race
end
class Race < ActiveRecord::Base
has_many eople
end

where @person.race.name should return "human" if you've populated the
race table and created a foreign key in person named "race_id".

--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.