gpt4 book ai didi

ruby-on-rails - 查询空多态关联

转载 作者:数据小太阳 更新时间:2023-10-29 08:11:01 24 4
gpt4 key购买 nike

我正在尝试查找所有没有关联BarFoo。我知道这行不通,但类似于 Foo.where(barable: nil)

Foo 模型

class Foo < ApplicationRecord
has_many :bars, as: :barable
end

条形模型

class Bar < ApplicationRecord
belongs_to :barable, polymorphic: true
end

表格

Foo(id: integer, name: string, created_at: datetime, updated_at: datetime)

Bar(id: integer, barable_type: string, barable_id: string, created_at: datetime, updated_at: datetime)

最佳答案

Foo.joins('LEFT JOIN bars ON bars.foo_id = foos.id').where('bars.id is null') 

关于ruby-on-rails - 查询空多态关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50457026/

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