Hello.
I've read about examples on inheritance with rails.
Here is an example:

http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/

The way is to add a type field in the table.
So if I have an Animal class with an attribute name, I can inherit
from this class like:

Dog < Animal, Cat < Animal, and so on.

With the type field in the table I can do Dog.all, Cat.all having
automaticaly all dogs, all cats, etc.
But what if I want to add some other attributes to Dog or Cat classes?
In the example above all classes have only name attribute.
What if I want inherit from Animal extending attributes and adding,
for example, an attribute like canFly? for inherited classes?

--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Jeremy Walker at May 3, 2012 at 2:42 pm

    On 3 May 2012 15:28, Mauro wrote:

    Hello.
    I've read about examples on inheritance with rails.
    Here is an example:


    http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/

    The way is to add a type field in the table.
    So if I have an Animal class with an attribute name, I can inherit
    from this class like:

    Dog < Animal, Cat < Animal, and so on.

    With the type field in the table I can do Dog.all, Cat.all having
    automaticaly all dogs, all cats, etc.
    But what if I want to add some other attributes to Dog or Cat classes?
    In the example above all classes have only name attribute.
    What if I want inherit from Animal extending attributes and adding,
    for example, an attribute like canFly? for inherited classes?
    Hi,

    You can add columns to the animals table that only some classes use. IMHO
    this is not a nice solution.

    I have written the SuperSTI gem that I think offers a nicer solution. Read
    about it at http://www.ihid.co.uk/projects/super_sti and
    https://github.com/ihid/super_sti

    Feel free to email me personally to discuss it further.

    Jeremy Walker
    http://www.ihid.co.uk

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to
    rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at
    http://groups.google.com/group/rubyonrails-talk?hl=en.
    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Mauro at May 3, 2012 at 2:49 pm

    On 3 May 2012 16:41, Jeremy Walker wrote:
    On 3 May 2012 15:28, Mauro wrote:

    Hello.
    I've read about examples on inheritance with rails.
    Here is an example:


    http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/

    The way is to add a type field in the table.
    So if I have an Animal class with an attribute name, I can inherit
    from this class like:

    Dog < Animal, Cat < Animal, and so on.

    With the type field in the table I can do Dog.all, Cat.all having
    automaticaly all dogs, all cats, etc.
    But what if I want to add some other attributes to Dog or Cat classes?
    In the example above all classes have only name attribute.
    What if I want inherit from Animal extending attributes and adding,
    for example, an attribute like canFly? for inherited classes?

    Hi,

    You can add columns to the animals table that only some classes use. IMHO
    this is not a nice solution.

    I have written the SuperSTI gem that I think offers a nicer solution. Read
    about it at
    http://www.ihid.co.uk/projects/super_sti and https://github.com/ihid/super_sti

    Feel free to email me personally to discuss it further.
    It creates a table for every inherited class.
    So in my example I must have a table animals, a table dogs and a table cats.
    It's the same if I create different models: Animal, Dog, Cat without
    using inheritance.

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Jeremy Walker at May 3, 2012 at 2:56 pm

    On 3 May 2012 15:49, Mauro wrote:
    On 3 May 2012 16:41, Jeremy Walker wrote:

    On 3 May 2012 15:28, Mauro wrote:

    Hello.
    I've read about examples on inheritance with rails.
    Here is an example:

    http://juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/
    The way is to add a type field in the table.
    So if I have an Animal class with an attribute name, I can inherit
    from this class like:

    Dog < Animal, Cat < Animal, and so on.

    With the type field in the table I can do Dog.all, Cat.all having
    automaticaly all dogs, all cats, etc.
    But what if I want to add some other attributes to Dog or Cat classes?
    In the example above all classes have only name attribute.
    What if I want inherit from Animal extending attributes and adding,
    for example, an attribute like canFly? for inherited classes?

    Hi,

    You can add columns to the animals table that only some classes use. IMHO
    this is not a nice solution.

    I have written the SuperSTI gem that I think offers a nicer solution. Read
    about it at
    http://www.ihid.co.uk/projects/super_sti and
    https://github.com/ihid/super_sti
    Feel free to email me personally to discuss it further.
    It creates a table for every inherited class.
    So in my example I must have a table animals, a table dogs and a table
    cats.
    It's the same if I create different models: Animal, Dog, Cat without
    using inheritance.

    No, it creates an animals table, and then creates extra linked tables for
    any extra attributes. So you'd create an animals table with all the shared
    attributes, then a dogs table that only has the extra attributes on there.
    So you don't have any duplication, and don't have a load of nullable
    columns on a super table.

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMay 3, '12 at 2:36p
activeMay 3, '12 at 2:56p
posts4
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Jeremy Walker: 2 posts Mauro: 2 posts

People

Translate

site design / logo © 2022 Grokbase