Ruby on rails 在RubyonRails 5.0中有很多关系

Ruby on rails 在RubyonRails 5.0中有很多关系,ruby-on-rails,ruby,ruby-on-rails-5,Ruby On Rails,Ruby,Ruby On Rails 5,我已经为Rails应用程序中的一个问题挣扎了一段时间。我有三个班,商品,商品分类和商品分类。MerchandisceCategory用于在其他两者之间创建多对多关系 在Rails控制台中运行以下命令时,会得到相应的结果: m = MerchandiseMerchandiseCategory.first # Returns an object that relates the first Merchandise # with the first Merchandise Category m.me

我已经为Rails应用程序中的一个问题挣扎了一段时间。我有三个班,商品商品分类商品分类。MerchandisceCategory用于在其他两者之间创建多对多关系

在Rails控制台中运行以下命令时,会得到相应的结果:

m = MerchandiseMerchandiseCategory.first
# Returns an object that relates the first Merchandise
# with the first Merchandise Category

m.merchandise_category
# Returns the corresponding merchandise_category

m.merchandise_category.merchandise_merchandise_categories.first
# Returns an array of all corresponding MerchandiseMerchandiseCategy ids

m.merchandise
# Returns the corresponding merchandise

m.merchandise.merchandise_merchandise_categories.first
# LoadError: Unable to autoload constant
# Merchandise::MerchandiseMerchandiseCategory, expected
# /home/bjarki/Development/h2/app/models/merchandise/merchandise_merchandise_category.rb
# to define it
因此,除了商品和商品类别之间的一对多关系之外,所有关系都起作用。我已经尝试了我能想到的一切,包括删除商品模型并重新创建它

这些是我正在学习的课程

商品。rb

# branch_id: uuid
# name: string
# price: integer
class Merchandise < ApplicationRecord
  has_many :merchandise_merchandise_categories
  has_many :categories, class_name: :MerchandiseCategory,
                        through: :merchandise_merchandise_categories
  belongs_to :branch
end
# branch_id: uuid
# name : string
class MerchandiseCategory < ApplicationRecord
  has_many :merchandise_merchandise_categories
  has_many :merchandises, through: :merchandise_merchandise_categories
  belongs_to :branch
end
# merchandise_id: uuid
# merchandise_category_id: uuid
class MerchandiseMerchandiseCategory < ApplicationRecord
  belongs_to :merchandise
  belongs_to :merchandise_category
end
#分支机构id:uuid
#名称:string
#价格:整数
类别商品<应用记录
有很多:商品商品类别
有很多:类别,类别名称::商品类别,
通过::商品\商品\类别
属于:分支机构
结束
商品\u分类.rb

# branch_id: uuid
# name: string
# price: integer
class Merchandise < ApplicationRecord
  has_many :merchandise_merchandise_categories
  has_many :categories, class_name: :MerchandiseCategory,
                        through: :merchandise_merchandise_categories
  belongs_to :branch
end
# branch_id: uuid
# name : string
class MerchandiseCategory < ApplicationRecord
  has_many :merchandise_merchandise_categories
  has_many :merchandises, through: :merchandise_merchandise_categories
  belongs_to :branch
end
# merchandise_id: uuid
# merchandise_category_id: uuid
class MerchandiseMerchandiseCategory < ApplicationRecord
  belongs_to :merchandise
  belongs_to :merchandise_category
end
#分支机构id:uuid
#名称:string
类别商品类别<应用记录
有很多:商品商品类别
有很多:商品,通过::商品
属于:分支机构
结束
商品\u商品\u类别。rb

# branch_id: uuid
# name: string
# price: integer
class Merchandise < ApplicationRecord
  has_many :merchandise_merchandise_categories
  has_many :categories, class_name: :MerchandiseCategory,
                        through: :merchandise_merchandise_categories
  belongs_to :branch
end
# branch_id: uuid
# name : string
class MerchandiseCategory < ApplicationRecord
  has_many :merchandise_merchandise_categories
  has_many :merchandises, through: :merchandise_merchandise_categories
  belongs_to :branch
end
# merchandise_id: uuid
# merchandise_category_id: uuid
class MerchandiseMerchandiseCategory < ApplicationRecord
  belongs_to :merchandise
  belongs_to :merchandise_category
end
#商品id:uuid
#商品类别识别号:uuid
类别商品商品类别<应用记录
属于:商品
属于:商品类别
结束
这几天我都快疯了。如果有人能为我指出正确的方向,我将不胜感激

商品\u商品\u类别.rb位于

应用程序/型号/商品/商品\商品\类别.rb


让我知道

我终于知道发生了什么,那是我文件的位置

我的应用程序包含相当多的模型,所以我决定为我的应用程序的不同组件创建几个目录

我放置这三个文件的目录名为商品,这显然是不允许的。我将目录重命名为商品组件,一切正常


如果有人能解释为什么不允许我将该目录命名为该名称,请在下面进行评论:-)

我知道这不是有问题的关系,但是如果您在
商品中更改了与此的关系:
有多个:类别,通过::商品(商品)类别,来源::商品(商品)类别
。又一次空中射击,你试过弹簧停止吗?有时我在使用旧的模型/控制器更改时遇到一些rails问题。谢谢你的建议,但两者都不起作用。我找到了解决方案,将在下面发布:-)这实际上与我的解决方案非常接近,我将发布它。谢谢你的建议:-)