Rails - Citier - 两级继承,Child Class不继承Middle Class属性

时间:2013-02-21 01:09:45

标签: ruby-on-rails citier multi-table-inheritance

我正在尝试实现一个结构:GrandFather< - Father< - Son 使用rails和gem citier。这个例子应该创建2个表来表示3个类:一个用于Root类(GrandFather)和它的属性,另一个用于表示父类和Son类(因为Son没有其他属性)和Father的属性。

class GrandFather < ActiveRecord::Base
    acts_as_citier
    attr_accessible :grand_father_attr, :type
end

class Father < GrandFather
    acts_as_citier
    attr_accessible :father_attr
end

class Son < Father

end

class CreateGrandFathers < ActiveRecord::Migration
    def change
        create_table :grand_fathers do |t|
            t.string :type
            t.string :grand_father_attr
            t.timestamps
        end
    end
end

class CreateFathers < ActiveRecord::Migration
    def up
        create_table :fathers do |t|
            t.string :father_attr
        end
        create_citier_view(Father)
    end

    def down
        drop_citier_view(Father)
        drop_table :fathers
    end
end

但是如果我打开rails console并输入'Son.new',那么GrandFather中的属性就在那里,但是来自Father类的属性丢失了:

1.9.3-p362 :001 > Son.new
citier -> Root Class
citier -> table_name -> grand_fathers
citier -> Non Root Class
citier -> table_name -> fathers
citier -> tablename (view) -> view_fathers
   (1.1ms)   SELECT COUNT(*)
 FROM pg_tables
 WHERE tablename = 'grand_fathers'


   (0.5ms)   SELECT COUNT(*)
 FROM pg_views
 WHERE viewname = 'grand_fathers'

 => #<Son id: nil, type: "Son", grand_father_attr: nil, created_at: nil, updated_at: nil> 

1 个答案:

答案 0 :(得分:0)

我最终得到了一个非常简单的解决方法,在我的示例中,唯一的修改是:

class Son < Father 
   acts_as_citier :table_name => 'fathers'
end