Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby rails converting params to int array

I am sending data via get and I need to put it into a int array to be used in a find. here is my code :

@found = Array.new
  params['candidate'].each do |c|
  @found << c.to_i
  end

My url looks like this

http://localhost:3000/export/candidate?candidate[]=3&candidate[]=4&commit=Export

If it makes any difference I am using it for this find

@candidate = Candidate.find(:all, :conditions => ["candidates.id IN ?", @found]) 

But currently it doesn't put it in a real array because I get this error

Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '4)' at line 1: SELECT * FROM `candidates` WHERE (candidates.id IN 4,2)

The brackets are missing around the array

Thanks and good morning!

Alex

like image 399
Alex Avatar asked Jul 18 '10 08:07

Alex


1 Answers

Just put parentheses around your ?

@candidate = Candidate.find(:all, :conditions => ["candidates.id IN (?)", @found]) 

Also, your first snippet can be collapsed down to:

@found = params['candidate'].map(&:to_i)
like image 199
Gareth Avatar answered Sep 28 '22 16:09

Gareth