Forum: Ruby string problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ada0fa9f326acbba88da65d76f675aca?d=identicon&s=25 Ganesh Ganesh (ganesh_bangalore)
on 2008-11-24 12:30
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
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-11-24 14:38
(Received via mailing list)
Hi --

On Mon, 24 Nov 2008, Ganesh Ganesh wrote:

> 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
C22abfaec867f9f138e98562b3624cd6?d=identicon&s=25 Michal Kurek (xoki)
on 2008-11-24 14:46
Ganesh Ganesh wrote:
> 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'))
This topic is locked and can not be replied to.