2

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

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 控制台并键入“Son.new”,则来自 GrandFather 的属性就在那里,但缺少来自父亲类的属性:

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> 
4

1 回答 1

0

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

class Son < Father 
   acts_as_citier :table_name => 'fathers'
end
于 2013-02-21T16:37:21.357 回答