4

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?

bcackerman
  • 1,486
  • 2
  • 22
  • 36
  • Possible duplicate of [ActiveRecord, has\_many :through, and Polymorphic Associations](http://stackoverflow.com/questions/1683265/activerecord-has-many-through-and-polymorphic-associations) – sbonami Mar 29 '16 at 21:30

1 Answers1

13

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
Community
  • 1
  • 1
Shannon
  • 1,073
  • 9
  • 22
  • just curious...why do we need the "has_many :businesses, through: :categorizations...." and "has_many :posts, through: :categorizations..." in the Category model? Why can't we just have "has_many :categorizations" ? – Rav Johal Aug 19 '14 at 10:06
  • @RavJohal I haven't tested this out, but my thoughts are that you don't actually have to have the last two `has_many` lines in the Category class, but you'd probably want them. All those last two lines do as I understand it is create helper methods on the Category class and allow you to do things like: `@category.businesses` or perhaps `@category.posts.where(published: true)` , etc. Without the last two has_many statements you wouldn't have these helper methods and would have to do something like this: `@category.categorizations.where(categorizable_type: 'Post')......`, basically much harder.. – FireDragon Feb 25 '15 at 22:48