开发者

parameters sql query in find by sql

How to pass parameters s开发者_Go百科ql query in find by sql


The documentation explains it all. You do it like this, where author_id and start_date are the parameters passed in.

Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]

http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-find_by_sql


There are several different ways of doing this

one way would be what Brendan has proposed.

for others I'll take Brendan's example itself

2 - Post.find(:all, :conditions => "author=#{author_id} and created=#{start_date}")

3 - Post.find_all_by_author_id_and_created(author_id, start_date)

and If you are using rails 3 even you can build your query

http://m.onkey.org/2010/1/22/active-record-query-interface

cheers

sameera

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜