ruby-on-rails - Rails如何使用join在另一个表中仅查找具有关联的has_one对象的对象
问题描述
嗨,我有一个模型 UnpairedProduct,我需要编写一个范围,仅显示未通过表名 feed_product 关联的 PairedProduct 的 UnpairedProducts。我尝试使用下面的代码执行此操作并收到此错误:NoMethodError: undefined method `table_name' for nil:NilClass
我还尝试了其他写作选项,不幸的是没有成功。你知道如何正确写吗?
未配对产品型号:
class UnpairedProduct < ApplicationRecord
belongs_to :feed_product
scope :resolved, -> { joins(feed_product: :paired_product).distinct.count }
scope :not_resolved, -> { joins(:feed_product).where.missing(:paired_product) } # PROBLEM HERE, THIS IS NOT WORKING
end
饲料产品型号
class FeedProduct < ApplicationRecord
has_many :unpaired_products
has_one :paired_product
end
配对产品型号
class PairedProduct < ApplicationRecord
belongs_to :feed_product
end
解决方案
推荐阅读
- cplex - 有没有办法在 CPLEX 中迭代二维数组
- reactjs - MaterialUI 数据网格加载动画无限循环
- discord.py - 在枕头上裁剪。(discord.py)
- java - JPA中的递归关系在postgresql下丢失但在h2数据库下保留
- api - 在 Snowflake 中调用 REST API
- perforce - 如何将 Perforce 移动到 Linux 服务器上的不同硬盘驱动器/SSD
- core-data - 统计满足特定条件的核心数据属性(SwiftUI)
- xml - 如何将多个文件内容插入 xml?
- go - 强制脚本验证标志失败(脚本评估没有错误,但以假/空顶部堆栈元素完成)
- c# - 为什么 targetFramework net472 项目不能引用 targetFrameworkVersion v4.7.2 项目?