I have been struggling with a problem in my Rails application for some time now. I have three classes: Products , MerchandiseCategory and MerchandiseMerchandiseCategory . MerchandiseMerchandiseCategory is used to create a many-to-many relationship between two others.
When I run the following commands in the Rails console, I get the corresponding results:
m = MerchandiseMerchandiseCategory.first
m.merchandise_category
m.merchandise_category.merchandise_merchandise_categories.first
m.merchandise
m.merchandise.merchandise_merchandise_categories.first
So, all relationships work, except for the one-to-many relationship between Merchandise and MerchandiseMerchandiseCategory. I tried everything I could think of, including deleting the Merchandise model and re-creating it.
These are the classes I work with.
merchandise.rb
class Merchandise < ApplicationRecord
has_many :merchandise_merchandise_categories
has_many :categories, class_name: :MerchandiseCategory,
through: :merchandise_merchandise_categories
belongs_to :branch
end
merchandise_category.rb
class MerchandiseCategory < ApplicationRecord
has_many :merchandise_merchandise_categories
has_many :merchandises, through: :merchandise_merchandise_categories
belongs_to :branch
end
merchandise_merchandise_category.rb
class MerchandiseMerchandiseCategory < ApplicationRecord
belongs_to :merchandise
belongs_to :merchandise_category
end
. , - .