how do you perform a SQL find in (?) call?
This doesn't seem to开发者_如何转开发 be working :
@zip = %w[07005, 07034, 07035]
CardSignup.find(:all, :conditions => ["zip_code IN (?)", @zip])
=> [ ]
However, if I do a simple find_by with the second zip code, it returns a result :
CardSignup.find_by_zip_code("07034")
=> Object<x01231 ..
What am I doing wrong ?
Your implementation of the array is incorrect. The way you have it at the moment the commas are part of each array object:
>> %w[07005, 07034, 07035]
=> ["07005,", "07034,", "07035"]
You should implement this with the commas removed:
>> %w{07005 07034 07035}
=> ["07005", "07034", "07035"]
or:
>> [07005, 07034, 07035]
=> [07005, 07034, 07035]
So the full implementation is:
@zip = %w{07005 07034 07035}
CardSignup.find(:all, :conditions => ["zip_code IN (?)", @zip])
=> [Object<x01231 ..
Don't use comma in %w[07005, 07034, 07035]
Just use %w[07005 07034 07035]
Try:
CardSignup.find_by_zip_code(@zip)
Edit (from comments):
How to select where ID in Array Rails ActiveRecord without exception
@zip = ["07005", "07034", "07035"]
CardSignup.find(:all, :conditions => ["zip_code IN (?)", @zip])
CardSignup.find_all_by_zip_code(["07005", "07034", "07035"])
精彩评论