Ruby on rails 3 具有多态关系的Rails-HABTM

Ruby on rails 3 具有多态关系的Rails-HABTM,ruby-on-rails-3,polymorphism,has-and-belongs-to-many,Ruby On Rails 3,Polymorphism,Has And Belongs To Many,我有一个表Category,其中可以有许多业务和帖子。一个业务/帖子可以有许多类别,因此我创建了一个名为类别关系的多态表来打破多对多关系 业务模型具有以下关系: has_many :categories, through: :category_relationships, :source => :category_relationshipable, :source_type => 'Business' has_many :category_relationships has

我有一个表
Category
,其中可以有许多
业务
帖子
。一个
业务
/
帖子
可以有许多
类别
,因此我创建了一个名为
类别关系
的多态表来打破多对多关系

业务
模型具有以下关系:

  has_many :categories, through: :category_relationships, :source => :category_relationshipable, :source_type => 'Business'
  has_many :category_relationships
has_many :category_relationships
  has_many :businesses, through: :category_relationships
  has_many :posts, through: :category_relationships
类别关系
模型具有以下关系:

 attr_accessible :category_id, :category_relationship_id, :category_relationship_type

  belongs_to :category_relationshipable, polymorphic: true
  belongs_to :business
  belongs_to :post
  belongs_to :category
类别
具有以下关系:

  has_many :categories, through: :category_relationships, :source => :category_relationshipable, :source_type => 'Business'
  has_many :category_relationships
has_many :category_relationships
  has_many :businesses, through: :category_relationships
  has_many :posts, through: :category_relationships
Post
Business
的关系类似

因此,现在当我运行
业务时。首先。categories
我得到错误:

Business Load (6.1ms)  SELECT "businesses".* FROM "businesses" LIMIT 1
  Business Load (2.5ms)  SELECT "businesses".* FROM "businesses" INNER JOIN "category_relationships" ON "businesses"."id" = "category_relationships"."category_relationshipable_id" WHERE "category_relationships"."business_id" = 3 AND "category_relationships"."category_relationshipable_type" = 'Business'
ActiveRecord::StatementInvalid: PG::Error: ERROR:  column category_relationships.business_id does not exist
LINE 1: ...lationships"."category_relationshipable_id" WHERE "category_...
                                                             ^
: SELECT "businesses".* FROM "businesses" INNER JOIN "category_relationships" ON "businesses"."id" = "category_relationships"."category_relationshipable_id" WHERE "category_relationships"."business_id" = 3 AND "category_relationships"."category_relationshipable_type" = 'Business'
我如何构建关系以使其工作?

这里有类似的问题: 在这里:

我认为应该是这样的:

class Category
  has_many :categorizations
  has_many :businesses, through: :categorizations, source: :categorizable, source_type: 'Business'
  has_many :posts, through: :categorizations, source: :categorizable, source_type: 'Post'
end

class Categorization
  belongs_to :category
  belongs_to :categorizable, polymorphic: true
end

class Business #Post looks the same
  has_many :categorizations, as: :categorizeable
  has_many :categories, through: :categorizations
end

只是好奇……为什么我们需要类别模型中的“has_many:business,through::categorizations…”和“has_many:posts,through::categorizations…”?为什么我们不能有“has\u many:categorizations”?RavJohal我还没有测试过,但我的想法是,你实际上不必在Category类中有最后两行,但你可能需要它们。据我所知,最后两行都是在Category类上创建helper方法,并允许您执行以下操作:
@Category.business
或者
@Category.posts.where(published:true)
,等等。如果没有最后两条语句,您就不会有这些帮助器方法,并且必须执行以下操作:
@category.categorizations.where(categorizable\u type:'Post')…
,基本上更难..可能的重复