【问题标题】:Rails query with has_many and belongs_to unexpected results带有 has_many 和 belongs_to 的 Rails 查询出乎意料的结果
【发布时间】:2019-01-25 03:19:17
【问题描述】:

我正在构建一个 Rails 应用程序并有一个名为 Item 的模型。它属于_to一个Type,并通过ItemTypes拥有_many Types。

当通过它们的 has_many 类型关联查询项目时,我期望没有结果,但是当它们属于该类型时,我会返回项目。

# models/item.rb
class Item < ApplicationRecord
  has_many :item_types
  has_many :types, through: :item_types
  belongs_to :type, inverse_of: :items
end

# models/type.rb
class Type < ApplicationRecord
  has_many :items, inverse_of: :item_type, dependent: :nullify
end

# models/itemtype.rb
class ItemType < ApplicationRecord
  belongs_to :item
  belongs_to :type
end

示例(预期行为):

food = Type.create
pizza = Item.create(type: food)

irb> pizza.type
food

irb> pizza.types
[]

但是,当我查询具有关联项目类型的项目并且预期没有结果时,即使没有 ItemType 对象,我也会取回我的项目。

irb> Item.where(type: food)
pizza
irb> Item.where(types: food)
pizza

(尽管 Pizza.types 返回 [],pizza 仍然返回)

我不确定幕后发生了什么,但我发现这是一个意想不到的行为。为什么会发生这种情况?如何让我的 Item.where(types: food) 查询不返回任何结果?

【问题讨论】:

  • 这就是全部。代码。你在运行吗? .new 不保存,您应该使用 .save 或调用 .create 而不是 new 来构建这些。我怀疑你看到的结果可能是因为你没有保存和持久化东西,所以关系的状态没有更新。另外,你能发布你的其他模型吗?
  • 谢谢,我已经添加了其他相关模型并更新了对 .create 的 .new 调用,因为它们确实存在于数据库中。在查询这些关系时,活动记录似乎在区分 has_many 类型和 belongs_to 类型时存在一些问题。
  • 我发现我可以通过编写更长的查询Item.includes(:item_types).where(item_type: {type: food}) 来获得我正在寻找的结果,但我仍然很想了解为什么原始查询不起作用以及为什么会这样has_many :types, through: :item_types 在 Item.where(types: food) 中不受尊重
  • 这仍然是您的确切型号吗?您需要从ApplicationRecord 继承,如class Item &lt; ApplicationRecord 与您在上面的 yoru 代码中所拥有的一样
  • 没有提供足够的信息来确定您看到的原因。我在下面的答案中提供了一个确实具有预期行为的工作示例。

标签: ruby-on-rails ruby ruby-on-rails-5


【解决方案1】:

您的模型不太正确;如果您希望它们与数据库一起使用,则需要从 ApplicationRecord 继承。您应该使用生成器来生成模型。

rails g model Item name:string
rails g model Type name:string
rails g model ItemType item:references type:references

然后,修改您的模型,使其看起来像这样:

class Item < ApplicationRecord
  has_many :item_types
  has_many :types, through: :item_types
end

class Type < ApplicationRecord
  has_many :item_types
  has_many :items, through: :item_types
end

class ItemType < ApplicationRecord
  belongs_to :item
  belongs_to :type
end

你的语法对于伪代码来说很好,但下面的代码应该可以工作

carbs = Type.create(name: 'Carbs')
pizza = Item.create(name: 'Pizza')
carbs.items << pizza

您还可以动态创建项目,如下所示:

carbs.items.create(name: 'Milkshake')

然后您可以访问以下内容:

carbs.items.first.name #=> 'Pizza'
pizza.types.map(&:name) #=> ['Carbs']

您需要查看association basics documentation 以了解其他功能。

【讨论】:

  • 这些模型并不能完全展示我试图解释的行为。项目有一个主要类型,在项目模型上定义为 belongs_to :type。它们还具有任意数量的关联类型,这些类型通过:item_types 定义为 has_many :types。当这两个定义都出现在 Item 模型上时,就会出现问题。
【解决方案2】:

我已经为这个问题找到了一个可行的解决方案,所以我会把它放在这里供以后的读者阅读。

问题是我们是否按类型或类型查询项目,在Item.where(type: food) 或Item.where(types: food) 类型中是模棱两可的,似乎是指在Item 模型中定义的belongs_to 关系与belongs_to :type。

要解决这个问题,要通过 ItemTypes 查询 Items,您可以首先使用 includes(:item_types) 连接表,然后能够查询它们的 Type 是所需实例的那些 ItemTypes。

给我所需结果的查询结果是Item.includes(:item_types).where(item_type: {type: food})。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    相关资源
    最近更新 更多