无效的 Controller 代码:
@mailings = Mailing.find(:all, :conditions => ["created_at.month = ?", m])
错误信息:
Mysql::Error: Unknown column 'created_at.month' in 'where clause': SELECT * FROM `mailings` WHERE (created_at.month = 10)
谢谢!
最佳答案
find
的:conditions
参数被直接内插到生成的 SQL 中。因此,您可以使用 SQL 命令。
如果您使用的是 MySQL,您可以尝试以下操作:
@mailings = Mailing.find(:all, :conditions => ["MONTH(created_at) = ?", m])
或者这个,对于 SQLite:
@mailings = Mailing.find(:all, :conditions => ["strftime('%m', created_at) = ?", m])
关于ruby-on-rails - 如何根据 'created_at'字段的月份设置条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7778984/