Ritesh Choudhary
Ritesh Choudhary

Reputation: 782

parameters sql query in find by sql

How to pass parameters sql query in find by sql

Upvotes: 8

Views: 13004

Answers (2)

sameera207
sameera207

Reputation: 16629

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

Upvotes: -2

Brendan Enrick
Brendan Enrick

Reputation: 4297

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

Upvotes: 33

Related Questions