Reputation: 6129
I'm working on a RoR project (RoR 3.1 and Ruby 1.9.2p290) where I've run into some serious strange problem with queries not finding things even if I know stuff should be found.
For simplicity, say there is a Car
model that has a color
attribute. I have several records in the db, all with color black. Getting all records and displaying them in a view works fine and I see nothing strange at all.
@cars = Car.all
Here's from the console where I get the first car and checks if the color equals black:
ruby-1.9.2-p290 :022 > Car.first.color == "Black"
Car Load (0.2ms) SELECT "cars".* FROM "cars" LIMIT 1
=> true
This works fine as you can see. Now if I try to query with find_by_color
it returns nil
! That shouldn't be.
ruby-1.9.2-p290 :021 > Car.find_by_color("Black")
Car Load (0.3ms) SELECT "cars".* FROM "cars" WHERE "cars"."color" = 'Black' LIMIT 1
=> nil
Same goes for any type of query where I check the color, e.g.
find(:all, :conditions => { :color => "Black" })
I have tried everything I can come up with to try, but at the moment I have no clue what might cause something like this.
Any idea what this might be or where to start looking?
I'm thankful for any help in resolving this before I go insane ;)
EDIT
The colors are from a CSV file I imported through a file upload and parsing it into Car records. Could there be something with the encoding or something like that? Also I use SQLite in development in case that helps.
UPDATE
I tried another query that works!!
Car.where('color like ?', "Black")
This returns all the records. Very strange. What might be the difference between these queries?
Upvotes: 2
Views: 588
Reputation: 57
Probably your color field has a strange non-printing character in the end, you must update field directly in database, cleaning up the extra characters.
Upvotes: 0
Reputation: 6129
The problem was encoding and when I added
value.encode('utf-8')
to each field for the CSV file I imported I got proper values into the database.
The encoding problem seems to affect the queries differently.
Upvotes: 1