gpt4 book ai didi

ruby-on-rails - Ruby 包含范围

转载 作者:行者123 更新时间:2023-12-05 00:45:31 25 4
gpt4 key购买 nike

在我的 Rails 6 应用程序中,我有定义 active 范围的模型 Journey:

class Journey < ApplicationRecord
has_many :users, through: :journey_progresses
has_many :journey_progresses, dependent: :destroy

scope :active, -> { where(is_deleted: false) }
end

在我的端点中,我想显示用户 Journey_progress 和序列化器旅程本身。为此,我使用下面的查询效果很好

      def query
current_user.journey_progresses.includes(:journey)
end

如何修改上述查询以仅显示事件旅程?我试图只添加 current_user.journey_progresses.includes(:journey).where(is_deleted: false) 的位置,但出现错误:

Caused by PG::UndefinedColumn: ERROR:  column journey_progresses.is_deleted does not exist

根据this answer我尝试使用 current_user.journey_progresses.includes(:journey).where(journey: {is_deleted: false} ) 但我遇到了另一个错误:

Caused by PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "journey"

这种操作的正确语法是什么?

最佳答案

您可以合并连接模型的范围。

current_user.journey_progresses.joins(:journey).merge(Journey.active)

这比测试 is_deleted bool 值更好,因为将来,如果使旅程处于事件状态的定义发生变化,则无需修改上述行。

关于ruby-on-rails - Ruby 包含范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63413788/

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