2013-05-11 71 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

主要最佳實踐這裏是正確地定義你的域模型,也不管Rails的這個擁有不錯的。

您需要決定PostPostTranslation具有什麼關係。如果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