【发布时间】:2020-07-06 12:45:34
【问题描述】:
我有一个信息模型:
class Information < ApplicationRecord
has_one :user, dependent: :restrict_with_exception
end
以及用户模型:
class User < ApplicationRecord
belongs_to :information, optional: true
end
查询用户在信息中的位置是什么的惯用方法是什么?
我已经试过了,它似乎不起作用:
Information.where(user: nil)
=> []
在我的开发数据库中,我希望它返回 1 条记录,因为我以用户为 nil 的方式设置了一条记录。任何帮助将不胜感激。
【问题讨论】:
-
Information.includes(:user).where(users: {information_id: nil})
标签: ruby-on-rails activerecord