Having a problem with my Model query
Pls i have the following code in my model
Letter.count(:id, :conditions => ["language_id = #{lang} AND :created_at => '#{start_date.to_date.strftime('%Y-%m-%d')}'..'#{end_date.to_date.strftime('%Y-%m-%d')}' " ])
开发者_开发技巧I am trying to get the count of letters.id of different letters between the given dates. This is the error an having...
Please those anyone know what am doing wrong...Thanks
SQLite3::SQLException: near ">": syntax error: SELECT COUNT("letters"."id") FROM "letters" WHERE ("letters".language_id = 1) AND (language_id = 1 AND :created_at => '2011-05-01'..'2011-05-08
This can be much simplified. A couple points:
- You don't use the
:created_at => ...
format within a string - You need to use
between ? and ?
for dates. - You don't need to manually
strftime
your dates, Rails will handle this automatically. - In Rails 3, the preferred way is to use
where(...)
instead of a:conditions
Hash for yourcount(...)
. - You should probably use Rails' safe interpolation for your
language_id
field too
Letter.where("language_id = ? AND created_at between ? and ?", lang, start_date.to_date, end_date.to_date).count
精彩评论