LoginSignup
4
2

More than 5 years have passed since last update.

モデルに hashメソッドを定義すると update出来なくなる

Posted at

小ネタですが、けっこうな時間はまったので、メモです。

こんな感じのエラーが出ました。

TypeError (no implicit conversion of String into Integer):
  app/controllers/hoges_controller.rb:59:in `block in update'
  app/controllers/hoges_controller.rb:58:in `update'

rails console でも Hogeのインスタンスで、updateが出来なくなっていました。
saveは出来るのでさっぱり???です。
結局、変更箇所をちょっとずつ戻しながら原因を特定しました。
犯人はこれでした。

app/models/hoge.rb
class Hoge < ActiveRecord::Base
  # (中略)

  def hash
    # hashとsaltを切り分けて hash返すような機能を実装してた
  end
end

Object#hash を上書きしてしまったが故の、謎のエラー発生だったのでした。
メソッド名を変更して無事解決しました。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2