开发者

How do you set a condition based on the month of the 'created_at' field

开发者 https://www.devze.com 2023-04-13 00:56 出处:网络
controller code that doesn\'t work: @mailings = Mailing.f开发者_如何学运维ind(:all, :conditions => [\"created_at.month = ?\", m])

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

0

精彩评论

暂无评论...
验证码 换一张
取 消