【问题标题】:Rails: Selecting user comments for the last dayRails:选择最后一天的用户评论
【发布时间】:2011-08-25 16:51:00
【问题描述】:

我有用户模型。我有评论模型。这是他们的关系:

User.rb:
  has_many :replies, :through => :items, :source => :comments

我正在尝试打印用户过去一天的所有回复。这样:

user.replies.find(:all, :conditions => [" created_at between ? AND ?", Time.zone.now.beginning_of_day, Time.zone.now.end_of_day])

这是我遇到的问题:

ruby-1.9.2-p180:038 > bob.replies.all(:conditions => [" created_at 之间 ? AND ?", Time.zone.now.beginning_of_day, Time.zone.now.end_of_day]) ActiveRecord::StatementInvalid: Mysql::Error: where 子句中的列 'created_at' 不明确:SELECT comments.* FROM comments INNER JOIN items ON comments.item_id = items.id WHERE ((items.user_id = 16)) AND ( created_at 之间 '2011-08-25 00:00:00' 和 '2011-08-25 23:59:59')

问题似乎是因为我的用户 - 评论关系。但我真的不明白发生了什么以及如何解决这个问题。感谢您的帮助。

【问题讨论】:

    标签: mysql ruby-on-rails ruby activerecord


    【解决方案1】:

    您需要在您的条件下将created_at 替换为comments.created_at。正如您在生成的查询中看到的,有两个表连接:commentsitems,它们都有created_at。所以你的db有点迷糊,就是你说的那个创建的。

    【讨论】:

      【解决方案2】:

      垃圾是对的;但是您也可以将其重写为:

      User.replies.find(:all, :conditions => {:created_at => Time.zone.now.beginning_of_day..Time.zone.now.end_of_day})
      

      【讨论】:

        猜你喜欢
        • 2012-04-16
        • 2018-03-16
        • 1970-01-01
        • 2011-02-23
        • 1970-01-01
        • 2015-04-17
        • 1970-01-01
        • 1970-01-01
        • 2013-06-04
        相关资源
        最近更新 更多