我有3个班:
1.文章
class Article < ActiveRecord::Base
has_many :categories_articles
has_many :subcategories_articles
has_many :categories, :through => :categories_articles
has_many :subcategories, :through => :subcategories_articles
end
2.Category
class Category < ActiveRecord::Base
has_many :articles
has_many :categories_articles
has_many :categories_subcategories
has_many :subcategories, :through => :categories_subcategories
has_many :articles, :through => :categories_articles
end
3.第三类是前两个的联合,category_article
class CategoryArticle < ActiveRecord::Base
belongs_to :category
belongs_to :article
end
所以,当我在视图中调用时
<% f.collection_select(:category_ids, Category.all, :id, :name, {include_blank:"selects"},{class:'form-control select2 multi', :required=>true, multiple: true}) %>
我收到此错误:
uninitialized constant Article::CategoriesArticle
类Subcategory和subcategory_article
也是如此答案 0 :(得分:3)
尝试
has_many :category_articles
和
has_many :subcategory_articles
你还必须改变这些:
has_many :categories, :through => :categories_articles
has_many :subcategories, :through => :subcategories_articles
类似于:
has_many :categories, :through => :category_articles
has_many :subcategories, :through => :subcategory_articles
Rails不会复合组合表名称的两个组件。只是最后一个组成部分。