string problem

G

Ganesh Ganesh

Hi How to find the resut by passing string as parameters in clause of
mysql query

For example
arr = ['t','a']
@user = User.find:)all,:conditions=>["firstname IN(#{arr.join(',')})"])
it is executing like
select * from users where firstname IN('t,a')
it returns 0 rows


Plz reply if anybody knows solution
 
D

David A. Black

Hi --

Hi How to find the resut by passing string as parameters in clause of
mysql query

For example
arr = ['t','a']
@user = User.find:)all,:conditions=>["firstname IN(#{arr.join(',')})"])
it is executing like
select * from users where firstname IN('t,a')
it returns 0 rows

You'll probably get more responses to ActiveRecord questions on the
Rails mailing lists. Meanwhile:

:conditions => ["firstname IN (?)", arr]


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
M

Michal Kurek

Ganesh said:
Hi How to find the resut by passing string as parameters in clause of
mysql query

For example
arr = ['t','a']
@user = User.find:)all,:conditions=>["firstname IN(#{arr.join(',')})"])

Docs: http://apidock.com/rails/ActiveRecord/Base/find/class

test666> arr = %w(a b)
=> ["a", "b"]
test666> User.find:)all, :conditions => { :firstname => arr} )
User Load (26.0ms) SELECT * FROM `users` WHERE (`users`.`firstname`
IN ('a','b'))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,616
Latest member
gijoji4272

Latest Threads

Top