2013-05-11 8 views
0

Я хочу иметь красивую и чистую структуру в своем приложении Rails.Rails 3 - Структура модели

Теперь у меня есть 4 файла в папке моделей: Post, PostTranslation, PostCategory и PostCategoryTranslation.

Это мой post.rb

class Post < ActiveRecord::Base 
    attr_accessible :image, :image_cache, :remove_image, :post_category_ids, :post_categories_attributes, :post_translations_attributes 
    validates :post_translations, :post_categories, presence: :true 

    translates :name, :content 
    has_many :post_translations, dependent: :destroy 
    accepts_nested_attributes_for :post_translations, allow_destroy: true 
end 

Это post_translation.rb

class PostTranslation < ActiveRecord::Base 
    attr_accessible :locale, :name, :content 
    validates :name, length: { maximum: 255 }, presence: true 
    validates :content, :locale, presence: true 

    belongs_to :post 

end 

Что я должен делать? Какая практика? Сделать почтовую папку и перенести перевод в эту папку и создать вспомогательную модель? Как это: class Translation < Post

Спасибо за ваши советы

ответ

0

Главный Best Practice здесь, чтобы определить вашу модель домена правильно, и это справедливо независимо от Rails.

Вам нужно решить, какое отношение имеет Post и PostTranslation имеют друг с другом. Если PostTranslation < Post, то belongs_to :post, вероятно, не должно быть там внутри PostTranslation.

Как только у вас есть более четкое моделирование, поместите все классы в папку models.

0

Я понял. Я добавил пространства имен блог ..

Теперь у меня есть эти файлы

blog/post.rb - Blog::Post 
blog/post/translation.rb - Blog::Post::Translation 
blog/category.rb - Blog::Category 
blog/category/translation.rb - Blog::Category::Translation 


class Blog::Post < ActiveRecord::Base 
    validates :translations, :categories, presence: true 
    translates :name, :content 
    accept_nested_attributes_for :translations, allow_destroy: true 
end 


class Blog::Post::Translation < Globalize::ActiveRecord::Translation 
    validates :name, presence: true 
    validates :locale, presence: true, uniqueness: { scope: :blog_post_id } 
end 
Смежные вопросы