【发布时间】:2017-08-02 10:31:24
【问题描述】:
我有两个模型:
class User < ActiveRecord::Base
has_many :ban_messages, dependent: :destroy
end
class BanMessage < ActiveRecord::Base
belongs_to :user
end
表BanMessage 包含一个字段t.datetime :ban_date,该字段存储了应该禁止用户的日期。 User 表包含一个字段status,其中包含状态值之一(活动、暂停、禁止)。当我向用户发送 BanMessage 时,他的状态字段从“活动”变为“暂停”。我也可以重新激活用户,这样他就不会在':ban_date' 被禁止,但他的 BanMessage 不会被破坏。
因此,我需要创建查询以选择所有状态为“已暂停”的用户,这些用户在其最新的 BanMessages 记录“ban_date”字段中具有 DateTime 值,该值介于 'DateTime.now.beginning_of_day' 和 'DateTime.now.end_of_day' 之间。
【问题讨论】:
-
您能否指定您要使用的 SQL 版本? - 到目前为止,您还可以粘贴您在查询中的尝试吗,任何表格和一些示例数据都会有所帮助
-
@jimmy8ball,我正在使用 ActiveRecord。
标签: ruby-on-rails activerecord