我有一个Rails7项目,在那里我有一些桌子
Pipelines个
class Pipeline < ApplicationRecord
has_many :states, inverse_of: :pipeline, dependent: :destroy
end
States个
class State < ApplicationRecord
belongs_to :pipeline, inverse_of: :states
has_many :items, inverse_of: :state
end
Items个
class Item < ApplicationRecord
belongs_to :state, inverse_of: :items
end
我编写了一个查询,以仅限制州上的特定项目
@pipeline = Pipeline.joins(states: :items)
.where(states: { items: { name: "Lorem" } })
.find_by(id: '123')
帖子主题:回复:
Pipeline Load (0.7ms) SELECT "pipelines".* FROM "pipelines" INNER JOIN "states" ON "states"."pipeline_id" = "pipelines"."id" INNER JOIN "items" ON "items"."state_id" = "states"."id" WHERE "items"."name" = $1 AND "pipelines"."id" = $2 LIMIT $3 [["name", "Lotem"], ["id", "123"], ["LIMIT", 1]]
但是,一旦我从管道调用状态,它就会无限制地生成新的查询:
puts @pipeline.states.inspect
State Load (0.4ms) SELECT "states".* FROM "states" WHERE "states"."pipeline_id" = $1 /* loading for inspect */ LIMIT $2 [["pipeline_id", "123"], ["LIMIT", 11]]
我期望它应该从它已经加载的集合中获取状态, 我错过了什么吗?