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.
Ganesh G. (Guest)
on 2008-11-24 13: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
David A. Black (Guest)
on 2008-11-24 15:38
(Received via mailing list)
Hi --

On Mon, 24 Nov 2008, Ganesh G. 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
Michal K. (Guest)
on 2008-11-24 15:46
Ganesh G. 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.