This project is archived and is in readonly mode.

#100 ✓invalid
Stefan Kroes

Problem combining single table inheritance and a has_many relation

Reported by Stefan Kroes | May 3rd, 2008 @ 02:22 PM

Hi all!

I think I've found a bug that occurs when combining single table inheritance with a has_many relation.

My models look something like this:

class Content < ActiveRecord::Base
  inheritance_column = 'inheritance_type'
  
  has_many :children, :class_name => 'Content', :foreign_key => 'parent_id'
end

class Page < Content
end

class Section < Content
end

When I have the following data in my database, for example:

{ id => 1, parent_id => nil, inheritance_type => 'Page' }

{ id => 2, parent_id => 1, inheritance_type => 'Section' }

{ id => 3, parent_id => 1, inheritance_type => 'Section' }

Page.find(:all)

Will return one page object and two section objects

Page.find(1).children

Will return two content objects while I'd expect it to return two section objects

Comments and changes to this ticket

  • Stefan Kroes

    Stefan Kroes May 3rd, 2008 @ 02:24 PM

    Page.find(:all)
    

    should be

    Content.find(:all)
    

    Apologies.

  • Stephen Celis

    Stephen Celis May 10th, 2008 @ 02:45 PM

    I don't think this is a bug.

    To use the assignment method, you need to explicitly call "self":

    self.inheritance_column = 'inheritance_type'
    

    Or, use this friendly method:

    set_inheritance_column = 'inheritance_type'
    

    Of course, once the inheritance_column is set, your has_many is going to break: I'm pretty sure it's only going to pick up objects with the class name "Content" (not "Section"), because the relationship isn't really polymorphic.

  • Stephen Celis

    Stephen Celis May 10th, 2008 @ 05:19 PM

    Oops, that second one isn't assignment:

    set_inheritance_column 'inheritance_type'
    
  • josh

    josh June 13th, 2008 @ 06:57 AM

    • State changed from “new” to “invalid”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages