# ActiveRecord abstract\_class and model inheritance

**URL:** <https://www.ruby-forum.com/t/activerecord-abstract-class-and-model-inheritance/177533>\
**Category:** Rails\
**Created:** [October 19, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/activerecord-abstract-class-and-model-inheritance/177533 "2009-10-19T22:47:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [October 19, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/activerecord-abstract-class-and-model-inheritance/177533/1 "2009-10-19T22:47:53Z")

</div>

Hi everybody,

I’m having problems on a testcase that I’m working on. I hope that I can  
explain the problem properly and that some of you can help me out  
becouse I’m realy stuck on this one.

Ok, I have a Page model that needs to be multi lingual. Therefore I have  
used a small but nice plugin: [GitHub - janne/model\_translations: Minimal implementation of Globalize2 style model translations](http://github.com/janne/model_translations)  
. Basically, I had to create a new model PageTranslation that conatains  
the i18n attributes. Let me draw a small example:

## Page

id  
parent\_id

## PageTranslation

title

Now I want to use these object for the base of other objects. So I used  
polymophisme on the page and page translation. Becouse of the  
inheritance, I have created an abstract model for Page and  
PageTranslation where other objects can inherit from. As an example  
object, I’ll use a new object StaticPage. This is what I have so far:

class Page \< ActiveRecord::Base  
has\_many :page\_translations  
belongs\_to :content, :polymorphic =\> true  
belongs\_to :parent, :class\_name =\> ‘Page’  
has\_many :children, :class\_name =\> ‘Page’, :foreign\_key =\> ‘parent\_id’  
end

class PageTranslation \< ActiveRecord::Base  
belongs\_to :page  
belongs\_to :content\_translation, :polymorphic =\> true  
end

class PageModel \< ActiveRecord::Base

self.abstract\_class = true

has\_one :page, :as =\> :content, :dependent =\> :destroy

validates\_presence\_of :page  
validates\_associated :page

after\_save :save\_page

translates :locale, :slug, :keywords, :description, :title, :content

Page.column\_names.each do |attribute|  
if attribute != “id”  
define\_method “#{attribute}=”.to\_sym do |value|  
init\_page  
self.page[“#{attribute}”] = value  
end

```
  define_method "#{attribute}".to_sym do
    self.page[attribute] if self.page
  end
end

```

end

def children  
self.page.children if self.page  
end

private

def init\_page  
self.page = Page.new unless self.page  
end

def save\_page  
self.page.save(false)  
end  
end

class PageTranslationModel \< ActiveRecord::Base  
self.abstract\_class = true

has\_one :page\_translation, :as =\> :content\_translation, :dependent =\>  
:destroy

validates\_presence\_of :page\_translation  
validates\_associated :page\_translation

after\_save :save\_page\_translation

PageTranslation.column\_names.each do |attribute|  
if attribute != “id”  
define\_method “#{attribute}=”.to\_sym do |value|  
init\_page\_translation  
self.page\_translation[“#{attribute}”] = value  
end

```
  define_method "#{attribute}".to_sym do
    self.page_translation[attribute] if self.page_translation
  end
end

```

end

private

def init\_page\_translation  
self.page\_translation = PageTranslation.new unless  
self.page\_translation  
end

def save\_page\_translation  
self.page\_translation.save(false)  
end  
end

class StaticPage \< PageModel  
end

class StaticPageTranslation \< PageTranslationModel  
end

For testing, I’ve created a simple form like this:

\<% form\_for(@static\_page, :url =\> static\_pages\_path , :html =\> { :id =\>  
“static-page-form” }) do |f| %\>  
  
  
\<% end %\>

This test works like a charm. The static page gets saved and the content  
get synchronized with the page table. So that part works.

The part I can’t get to work, is the part with the translations.  
I have used the model translation plugin on a simpler test case and got  
it to work by adding the following to a model:  
has\_many :test\_translations  
after\_update :save\_translations

def new\_translation\_attributes=(translation\_attributes)  
translation\_attributes.each do |attributes|  
test\_translations.build(attributes)  
end  
end

def existing\_translation\_attributes=(translation\_attributes)  
test\_translations.reject(&:new\_record?).each do |translation|  
attributes = translation\_attributes[translation.id.to\_s]  
if attributes  
test\_translation.attributes = attributes  
else  
test\_translations.destroy(translation)  
end  
end  
end

def save\_translations  
test\_translations.each do |translation|  
translation.save(false)  
end  
end  
And have my form fields like this:

But I don’t seem to be able to implement this pattern in my abstract  
PageModel.  
It would be great if someone could help me out with this one, becouse I  
don’t think I would be able to solve this without any help.

Thank you in advance

---

<div class="post-metadata">

**Author:** ![roa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8e7dd6/32.png) [@roa](https://www.ruby-forum.com/u/roa)\
**Post date:** [October 20, 2009, 9:14pm UTC](https://www.ruby-forum.com/t/activerecord-abstract-class-and-model-inheritance/177533/2 "2009-10-20T21:14:45Z")

</div>

anyone?
