gpt4 book ai didi

ruby-on-rails - Rails .includes() 返回 'undefined method'

转载 作者:行者123 更新时间:2023-12-02 15:19:02 25 4
gpt4 key购买 nike

我有一个带有关联位置的公司对象。

公司有_many :locations地点 belongs_to :company

当我打电话时:

@company = Company.find(5).includes(:locations)

我得到错误:

NoMethodError: undefined method `includes' for #<Company:0x000000066ad398>

据我所知,我正在按照 ruby​​onrails.org 的说明进行操作。

最佳答案

find() 触发数据库查询,并返回模型的一个实例。您不能对其调用 includes,因为 includes 应该有一个 ActiveRecord::Relation 作为接收者。

你需要颠倒顺序:find 必须留在最后:

@company = Company.includes(:locations).find(5)

关于ruby-on-rails - Rails .includes() 返回 'undefined method',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38618184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com