petehamilton / citier

CITIER (Class Inheritance & Table Inheritance Embeddings for Rails) is a solution for simple Multiple Class Inheritance in Rails.
88 stars 24 forks source link

STI class not inheriting attributes from citier child class #67

Open maliakmal opened 11 years ago

maliakmal commented 11 years ago

Hi guys I have the following classes set up:

class Contact < ActiveRecord::Base
acts_as_citier
end

class Company < Contact
acts_as_citier
end

class Operator < Company
end

When I try to instantiate an object of Company I get an object with all the attributes of Company class and Contact however if I try to instantiate an object of class Operator I only get the attributes of class Contact and not of Company which I'm inheriting from. I've dropped and created my citier views however still can't resolve this. Please do help.

zubairshams commented 11 years ago
 class Content < ActiveRecord::Base
  acts_as_citier
  validates :page_id, presence: true
 end

 class CommonField < Content
   acts_as_citier
   self.inheritance_column = 'sub-type'
 end

 class Text < CommonField 
 end

I'm also having problems with STI. In my case, STI child class able to inherit attributes from both Content and CommonField but when i save it, Content is saving the type of STI class as nil rather than CommonField.

1.9.2p320 :006 > t = Text.new(page_id: 1) citier -> Root Class citier -> table_name -> contents citier -> Non Root Class citier -> table_name -> common_fields citier -> tablename (view) -> view_common_fields => #<Text id: nil, page_id: 1, type: nil, subtype: "Text", description: nil, url: nil, created_at: nil, updated_at: nil> 1.9.2p320 :007 > t.save! citier -> SAVING Text citier -> Attributes for CommonField: {"id"=>nil, "page_id"=>1, "type"=>nil, "subtype"=>"Text", "description"=>nil, "url"=>nil, "created_at"=>nil, "updated_at"=>nil} citier -> Changed attributes for CommonField: ["subtype", "page_id", "id"] citier -> Attributes for Text: {} citier -> Changed attributes for Text: [] citier -> SAVING CommonField citier -> Attributes for Content: {"id"=>nil, "page_id"=>1, "type"=>nil} citier -> Changed attributes for Content: ["page_id", "id"] citier -> Attributes for CommonField: {"subtype"=>"Text", "description"=>nil, "url"=>nil, "created_at"=>nil, "updated_at"=>nil} citier -> Changed attributes for CommonField: "subtype" BEGIN SQL (0.2ms) INSERT INTO contents (page_id, type) VALUES (1, NULL) (49.8ms) COMMIT (0.2ms) BEGIN SQL (0.2ms) INSERT INTO common_fields (created_at, description, id, subtype, updated_at, url) VALUES ('2013-02-21 10:17:27', NULL, 10, 'Text', '2013-02-21 10:17:27', NULL) (44.4ms) COMMIT => true

gustavobap commented 11 years ago

I have exactly the same problem as @maliakmal, I had not read this post before so I started a new issue https://github.com/PeterHamilton/citier/issues/68. I use an ugly workaround to make the child class inherit the middle class attributes.

zubairshams commented 11 years ago

This gem does not seem to be maintained, in the end i used Multiple Table Inheritance with AR.

gustavobap commented 11 years ago

@zubairshams, is this a gem ? can you paste the link to it ?

zubairshams commented 11 years ago

@gustavobap No i'm not using any gem, you can achieve this without the gem. But there are some gem for achieving this.

https://github.com/hzamani/acts_as_relation https://github.com/BenjaminMedia/Heritage

But i recommend you should do this without using any gem. There is a great help to achieve Multiple table inheritance in this article. http://techspry.com/ruby_and_rails/multiple-table-inheritance-in-rails-3/

This way you can also customize it according to your needs.

gustavobap commented 11 years ago

Hi, @zubairshams thanks for the answer, I saw other solutions but they are based on simulating MTI, I want to actually inherit the model classes like with Rails STI. I ended up with a very simple workaround, in @maliakmal example the only modification would be:

 class Text < CommonField 
     acts_as_citier :table_name => 'common_fields'
 end