Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Problem combining single table inheritance and a has_many relation

#100

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

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Stefan Kroes
    Stefan Kroes
    Page.find(:all)
    

    should be

    Content.find(:all)
    

    Apologies.

    May 3rd, 2008 @ 02:24 PM

  2. Stephen Celis
    Stephen Celis

    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.

    May 10th, 2008 @ 02:45 PM

  3. Stephen Celis
    Stephen Celis

    Oops, that second one isn't assignment:

    set_inheritance_column 'inheritance_type'
    

    May 10th, 2008 @ 05:19 PM

  4. josh
    josh
    • State changed from new to invalid

    June 13th, 2008 @ 06:57 AM