Ruby on Rails 查找没有记录的地方
我有以下关联:
Outlet => has_many :checkins
Checkin => belongs_to :outlet
每天,Outlet 需要获得 1 次签入。 我可以通过以下方式获取今天签到的网点列表:
Checkin.where('DATE(created_at) = ?', Date.Today)
但是,我不确定如何获取今天尚未签到的网点列表。
因此,如果商店 A 今天创建了签到记录,但商店 B 没有签入,因此没有创建记录,则商店 B 将被返回.
如果我们这样说:
@checkin_outlet_name = Checkin.where('DATE(created_at) = ?', Date.Today).outlet.name
@outlet = Outlet.all
理论上我可以找到今天没有签到的所有奥特莱斯,因为 @outlet 中而不是 @checkin_outlet_name 中的所有记录都将是我正在寻找的人。
但是,我不知道如何在 Rails 中实现这一点。
任何帮助将不胜感激。
I have the following associations:
Outlet => has_many :checkins
Checkin => belongs_to :outlet
Every day, and Outlet needs to get 1 checkin.
I can get the list of outlets with a checkin made today with this:
Checkin.where('DATE(created_at) = ?', Date.Today)
However, I am not sure about how I could get a list of outlets that have not had a checkin associated with them today.
So if Outlet A had a check in record created today, but Outlet B had not had a check in, and hence no record made, Outlet B would be returned.
If we say that:
@checkin_outlet_name = Checkin.where('DATE(created_at) = ?', Date.Today).outlet.name
@outlet = Outlet.all
I would theoretically be able to find all the Outlets without checkins made today, because all the records that are in @outlet and NOT on @checkin_outlet_name would be the ones I am looking for.
However, I have no idea how to achieve this in Rails.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要类似
Outlet.joins(:checkins).where("DATE(checkins.created_at) <> ?", Date.today)
You probably want something like
Outlet.joins(:checkins).where("DATE(checkins.created_at) <> ?", Date.today)
如果您想在不编写复杂查询的情况下执行此操作,那么
在数据库中执行此操作的更好方法
If you want to do it without writing complex queries,
Better way to do it in DB