Bitwise
Bitwise

Reputation: 8461

Finding the instance of a model not true:class

I currently have a method that returns true or false if a record exists?. It does it's job find but I need to return that record that it found and update it with new information. The code I have right now does not as I want it to. Here is my code

Model method:

def self.stored?(image_path)
  md5 = Digest::MD5.file(image_path).hexdigest

  FailedImage.where(md5: md5).exists?
end

This returns a true class. I want it to return a record.

controller

if failed_image = FailedImage.stored?(context.download_path)
  failed_image.update_from_image_optimization(context.image_optimization)
end

I get this error undefined method update_from_image_optimization for true:TrueClass This is because it is returning true class when I want a record. Anybody know how I can achieve this?

Upvotes: 0

Views: 45

Answers (1)

dp7
dp7

Reputation: 6749

Remove exists? from FailedImage.where(md5: md5).

This would return you an array of records.

Model method should look as follows:

def self.stored?(image_path)
  md5 = Digest::MD5.file(image_path).hexdigest
  FailedImage.where(md5: md5)
end

Upvotes: 1

Related Questions