Proper SQL syntax in rails for doing a find on objects created after X datetime
I can't seem to get my syntax correct..perhaps this is an easy solve.
I am trying to find all comment objects created after last_checked_mail
I tried..
current_user.comments.find(:all, :conditions => ["created_at > ?, current_user.last_checked_mail"])
But that returns :
ActiveRecord::Prepar开发者_开发问答edStatementInvalid: wrong number of bind variables (0 for 1)
You end the string in the wrong place. It should be:
["created_at > ?", current_user.last_checked_mail]
The error message is saying that you don't have a variable to back up your '?' in the query. For each '?' in your query string, you need to have an object in the array that gets bound to that question mark. Since you have one '?' in your query, rails is expecting to find one "bind variable" to bind that question mark to.
This is because your string swallowed up the variable you intended to use by mistake.
try this.
current_user.comments.find(:all, :conditions => ["created_at > ?", current_user.last_checked_mail])
You need to put the variables outside the quotes..
精彩评论