controller code that doesn't work:
@mailings = Mailing.f开发者_如何学运维ind(:all, :conditions => ["created_at.month = ?", m])
Error msg:
Mysql::Error: Unknown column 'created_at.month' in 'where clause': SELECT * FROM `mailings` WHERE (created_at.month = 10)
thanks!
The :conditions
argument of find
is interpolated directly into the resulting SQL. Thus, you can use SQL commands.
You could try the following if you're using MySQL:
@mailings = Mailing.find(:all, :conditions => ["MONTH(created_at) = ?", m])
Or this, for SQLite:
@mailings = Mailing.find(:all, :conditions => ["strftime('%m', created_at) = ?", m])
Try:
@mailings = Mailing.find(:all, :conditions => ["MONTH(created_at) = ?", m])
Assuming you're using MySQL you can find more in the Docs: MySQL Date and Time Functions
精彩评论