开发者

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:

  1. You don't use the :created_at => ... format within a string
  2. You need to use between ? and ? for dates.
  3. You don't need to manually strftime your dates, Rails will handle this automatically.
  4. In Rails 3, the preferred way is to use where(...) instead of a :conditions Hash for your count(...).
  5. 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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜