Simple Active Record Query fails on Ruby 1.9.2 not on ree-1.8.2

Hi,

I have this small problem that made me really frustrating for 3 hours.

the code is simple. Im using rails 3.1. it uses paperclip gem to manage attachment. this code works fine on production and development either with mri 1.9.2 or ree 1.8.7, the problem is when i test it using cucumber

here is the code

def create

@photo = Photo.new(params[:photo])

@photo.md5sum = @photo.file.fingerprint

@duplicate = Photo.find_by_md5sum(@photo.md5sum)

@test = Photo.all

if @duplicate

flash[:error] = “Found duplicate entry”

else

@result = @photo.save

end

end

it just check for the md5sum of the photo, and save if it’s not duplicate.

i test it using cucumber, the problem is with Photo.find_by_md5sum. it return nil. but the @test return all of the record, including the record with the md5sum that should be matched.

i also try to changed it with Photo.where(:md5sum => @photo.md5sum) but on 1.9.2 its return to .

but funny, when I change it using like cause, Photo.where(“md5sum like ?”, @photo.md5sum) it return the right object.

the only problem if i try to use equality in the query. it always returns nil on 1.9.2. when i test it using ree 1.8.2 everything works fine.

I have no idea how this happend, maybe some one here face the same problem ?

Hi, I have this small problem that made me really frustrating for 3 hours. the code is simple. Im using rails 3.1. it uses paperclip gem to manage attachment. this code works fine on production and development either with mri 1.9.2 or ree 1.8.7, the problem is when i test it using cucumber here is the code def create @photo = Photo.new(params[:photo])

@photo.md5sum = @photo.file.fingerprint @duplicate = Photo.find_by_md5sum(@photo.md5sum)

@test = Photo.all

if @duplicate flash[:error] = "Found duplicate entry" else @result = @photo.save end end it just check for the md5sum of the photo, and save if it's not duplicate. i test it using cucumber, the problem is with Photo.find_by_md5sum. it return nil. but the @test return all of the record, including the record with the md5sum that should be matched.

Have a look in the log to see what query is being generated for the find_by_md5sum and see if anything looks odd.

Also you could use ruby-debug to break in at the find_by_md5sum and inspect the data to make sure it looks ok. In the console then use find(6) or whatever is the id of the existing duplicate record and then inspect it's sum and the sum of the new one you have just made. I presume these are all string types?

i also try to changed it with Photo.where(:md5sum => @photo.md5sum) but on 1.9.2 its return to . but funny, when I change it using like cause, Photo.where("md5sum like ?", @photo.md5sum) it return the right object.

Are you sure there is not a space or something at the end of the one in the database or in the query so Like finds it but equality does not.

the only problem if i try to use equality in the query. it always returns nil on 1.9.2. when i test it using ree 1.8.2 everything works fine.

I have no idea how this happend, maybe some one here face the same problem ?

In the rails console play about with the find, putting in the md5sum manually for example, to see what happens.

Colin

Here i’ve manage to debug the script running cucumber.

def create

@photo = Photo.new(params[:photo])

@photo.status = ‘Uncategorized’ if !user_signed_in?

@photo.md5sum = @photo.file.fingerprint

@duplicate = Photo.find_by_md5sum(@photo.md5sum)

=> debugger

ruby-1.9.2-p180 :012 > @photo

=> #<Photo id: nil, owner_id: “owner_id 2”, status: “Uncategorized”, note: “note 2”, created_at: nil, updated_at: nil, file_file_name: “rails.png”, file_content_type: “image/png”, file_file_size: 79406, file_updated_at: “2011-07-17 03:51:34”, md5sum: “4f9c652dfa2b307ce11efe85afcef98b”, width: 300, height: 356>

ruby-1.9.2-p180 :011 > Photo.all

=> [#<Photo id: 1, owner_id: “owner_id 1”, status: “Uncategorized”, note: “note 1”, created_at: “2011-07-17 03:51:33”, updated_at: “2011-07-17 03:51:33”, file_file_name: “rails.png”, file_content_type: “image/png”, file_file_size: 79406, file_updated_at: “2011-07-17 03:43:56”, md5sum: “4f9c652dfa2b307ce11efe85afcef98b”, width: 300, height: 356>]

CACHE (0.0ms) SELECT “photos”.* FROM “photos”

ruby-1.9.2-p180 :014 > Photo.find_by_md5sum(@photo.md5sum)

=> nil

CACHE (0.0ms) SELECT “photos”.* FROM “photos” WHERE “photos”.“md5sum” = ‘4f9c652dfa2b307ce11efe85afcef98b’ LIMIT 1

ruby-1.9.2-p180 :015 > Photo.where(:md5sum => “4f9c652dfa2b307ce11efe85afcef98b”)

=>

CACHE (0.0ms) SELECT “photos”.* FROM “photos” WHERE “photos”.“md5sum” = ‘4f9c652dfa2b307ce11efe85afcef98b’

ruby-1.9.2-p180 :018 >Photo.where(“md5sum = ?”, “4f9c652dfa2b307ce11efe85afcef98b”)

=>

CACHE (0.0ms) SELECT “photos”.* FROM “photos” WHERE (md5sum = ‘4f9c652dfa2b307ce11efe85afcef98b’)

ruby-1.9.2-p180 :019 > Photo.where(“md5sum like ?”, “4f9c652dfa2b307ce11efe85afcef98b”)

=> [#<Photo id: 1, owner_id: “owner_id 1”, status: “Uncategorized”, note: “note 1”, created_at: “2011-07-17 03:51:33”, updated_at: “2011-07-17 03:51:33”, file_file_name: “rails.png”, file_content_type: “image/png”, file_file_size: 79406, file_updated_at: “2011-07-17 03:43:56”, md5sum: “4f9c652dfa2b307ce11efe85afcef98b”, width: 300, height: 356>]

CACHE (0.0ms) SELECT “photos”.* FROM “photos” WHERE (md5sum like ‘4f9c652dfa2b307ce11efe85afcef98b’)

it’s really confusing, does it’s do anything with internal cache ?

Ahmy Yulrizka

Please don't top post, it makes it difficult to follow the thread. Insert your reply at appropriate points in the previous message. Thanks.

Here i've manage to debug the script running cucumber. def create @photo = Photo.new(params[:photo])

@photo\.status = &#39;Uncategorized&#39; if \!user\_signed\_in?

@photo\.md5sum = @photo\.file\.fingerprint
@duplicate = Photo\.find\_by\_md5sum\(@photo\.md5sum\)

=&gt; debugger

... ruby-1.9.2-p180 :012 > @photo => #<Photo id: nil, owner_id: "owner_id 2", status: "Uncategorized", note: "note 2", created_at: nil, updated_at: nil, file_file_name: "rails.png", file_content_type: "image/png", file_file_size: 79406, file_updated_at: "2011-07-17 03:51:34", md5sum: "4f9c652dfa2b307ce11efe85afcef98b", width: 300, height: 356>

ruby-1.9.2-p180 :011 > Photo.all => [#<Photo id: 1, owner_id: "owner_id 1", status: "Uncategorized", note: "note 1", created_at: "2011-07-17 03:51:33", updated_at: "2011-07-17 03:51:33", file_file_name: "rails.png", file_content_type: "image/png", file_file_size: 79406, file_updated_at: "2011-07-17 03:43:56", md5sum: "4f9c652dfa2b307ce11efe85afcef98b", width: 300, height: 356>] CACHE (0.0ms) SELECT "photos".* FROM "photos"

ruby-1.9.2-p180 :014 > Photo.find_by_md5sum(@photo.md5sum) => nil CACHE (0.0ms) SELECT "photos".* FROM "photos" WHERE "photos"."md5sum" = '4f9c652dfa2b307ce11efe85afcef98b' LIMIT 1

What happens if you put this sql directly into you database manager (using mysql command or whatever)?

Could this be a Rails 3.1 bug?

Colin

Also I think you said it is ok with ree 1.8.2, do you see anything different in the queries if you repeat your debug tests with that?

Colin