找到模型的实例不是真的:class

时间:2016-12-14 03:00:48

标签: ruby-on-rails ruby

我目前有一个方法,如果存在记录,则返回true或false?它找到了它的工作但我需要返回它找到的记录并用新信息更新它。我现在的代码并不像我想要的那样。这是我的代码

模型方法:

     23 for index, key in enumerate(d):
---> 24         fig.append(d[key], index+1, 1)
     25 plot(fig)
     26

TypeError: 'NoneType' object is not callable

这会返回一个真正的类。我想让它返回一条记录。

控制器

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

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

我收到此错误if failed_image = FailedImage.stored?(context.download_path) failed_image.update_from_image_optimization(context.image_optimization) end 这是因为当我想要一条记录时它会返回真正的类。谁知道我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

exists?移除FailedImage.where(md5: md5)

这会返回一组记录。

模型方法应如下所示:

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