views:

47

answers:

1

class Parent < ActiveRecord::Base
end

class Sub < Parent
end

class SubSub < Sub
end

>> SubSub.create :name => 'name1'
>> SubSub.create :name => 'name2'
>> SubSub.create :name => 'name3'

Then


>> SubSub.all.map{|x| x.name}    # => ['name1', 'name2', 'name3']
>> Sub.all.map {|x| x.name}      # => []  # I was expected that it will show all items;
>> Parent.all.map { |x| x.name}  # => ['name1', 'name2', 'name3']

I need Sub.all to show all its subclass's items, how to make it? It's this a bug?


I tested again, and it did work when there is no 'type' column specified in the table, but fails when with the 'type' column.


There is just one table named 'parents' with 'type' column;


My Env: rails-3.0.0.beta3, ruby-1.9.2-pre

A: 

Sub.all DOES show all its subclass' items. I suggest you check your code. DEFINITELY not a bug.

Shreyas Satish
Thanks, I test again, and it did work when there is no 'type' column specified in the table, but fails when with the 'type' column.
Croplio
You have to have the 'type ' column. Otherwise Rails will not be able to exhibit STI behavior.
Shreyas Satish
Yes, so the problem apears.
Croplio
By any chance you havent created tables for Sub and Subsub , have you?
Shreyas Satish
Nope, just one table named 'parents' with type field, but could I create multi tables for STI ?
Croplio
No, you shouldn't create multiple tables. Just one table.
Shreyas Satish