0
我产生命名空间中的模型,我怎么可以设置很多一对多
关系,类别有很多帖子,帖子有很多类许多一对多的模型关系
rails g model Blog::Post body:text, title:string
rails g model Blog::Category title:string
rails g model Blog::CategoryPost post_id:integer, category_id:integer
和我的模型看起来像
class Blog::Category < ActiveRecord::Base
attr_accessible :title
has_many :posts, :class_name => 'Blog::Post', :through => :blog_categories_posts
end
class Blog::CategoryPost < ActiveRecord::Base
belongs_to :category, :class_name => 'Blog::Category'
belongs_to :post, :class_name => 'Blog::Post'
end
class Blog::Post < ActiveRecord::Base
attr_accessible :body, :title
has_many :categories, :class_name => 'Blog::Category', :through => :blog_categories_posts
end