具有多态关系的Rails HABTM [英] Rails HABTM with polymorphic relationships
问题描述
我有一个表Category
,其中可以有许多Businesses
和Posts
.一个Business
/Post
可以有多个Categories
,因此我创建了一个名为CategoryRelationship
的多态表,以打破多对多的关系.
Business
模型具有以下关系:
has_many :categories, through: :category_relationships, :source => :category_relationshipable, :source_type => 'Business'
has_many :category_relationships
CategoryRelationship
模型具有以下关系:
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
Category
具有以下关系:
has_many :category_relationships
has_many :businesses, through: :category_relationships
has_many :posts, through: :category_relationships
Post
与Business
具有相似的关系.
所以现在当我运行Business.first.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'
我如何构建关系以使其正常工作?
此处有类似问题: Rails多态has_many:through 在这里: ActiveRecord,has_many:through和多态关联 >
我认为应该是这样的:
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
I have a table Category
which can have many Businesses
and Posts
. And a Business
/Post
can have many Categories
so I created a polymorphic tabled called CategoryRelationship
to break up the many to many relationship.
Business
model has these relationships:
has_many :categories, through: :category_relationships, :source => :category_relationshipable, :source_type => 'Business'
has_many :category_relationships
CategoryRelationship
model has these 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
Category
has these relationships:
has_many :category_relationships
has_many :businesses, through: :category_relationships
has_many :posts, through: :category_relationships
Post
would have similar relationships as Business
.
So now when I run Business.first.categories
I get the error:
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'
How do I structure the relationships so this works?
Similar questions here: Rails polymorphic has_many :through And here: ActiveRecord, has_many :through, and Polymorphic Associations
I think it should be something like this:
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
这篇关于具有多态关系的Rails HABTM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!