0

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 ?

Trip
  • 26,756
  • 46
  • 158
  • 277

3 Answers3

3

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 ..
Douglas F Shearer
  • 25,952
  • 2
  • 48
  • 48
2
Don't use comma in %w[07005, 07034, 07035]

Just use  %w[07005 07034 07035]
Ashish
  • 5,723
  • 2
  • 24
  • 25
0

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"])
Community
  • 1
  • 1
Reuben Mallaby
  • 5,740
  • 4
  • 47
  • 45