LoginSignup
2
0

More than 5 years have passed since last update.

【凡ミス禄】カラム名をupdateにしたらDangerousAttributeErrorで怒られた

Last updated at Posted at 2018-02-05

よっしゃ!scaffoldで基本機能出来たし、一式チェックしてみようかな~と思い、とりま新規投稿。
すると

ActiveRecord::DangerousAttributeError (update is defined by Active Record. 
Check to make sure that you don't have an attribute or method with the same name.):

怒られた!
そこで、こちらの親切な記事を参考にさせて頂きました。
本厚木のエンジニアブログではある

どうやら、カラム名には規約があるらしく、

/db/schema.rb
  create_table "contributions", force: :cascade do |t|
    t.string "title"
    t.string "user_name"
    t.text "overview"
    t.text "category"
    t.date "update"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

'update'はNGだそうな。
最終登録日時なのでupdateが適切かと思ったのですが、今回はmodifiedに変更したい。
なので、rename用の新たなマイグレーションファイルを作成。

$ rails g migration rename_Date_column_to_tests

そすると、

db/migrate/20180205081916_rename_date_column_to_tests.rb
class RenameDateColumnToTests < ActiveRecord::Migration[5.1]
  def change
  end
end

change関数ができてるので

db/migrate/20180205081916_rename_date_column_to_tests.rb
class RenameDateColumnToTests < ActiveRecord::Migration[5.1]
  def change
    #追記
    rename_column :contribution, :update, :modified
  end
end

(あれ?テーブル名って確かcontributionで設定したよな?うろ覚えだけど、まいっか!)
update → modifiedにrenameするべく、もう一度migrate。

$ rake db:migrate



ActionController::RoutingError (No route matches [GET] "/contribution"):

ルーティングエラー。
えーと、「テーブル名」ってそもそもなんだっけ?
とよくよく考えた結果、

/db/schema.rb
  create_table "contributions", force: :cascade do |t|
省略

contributions

s抜けてましたね。

これで解決!

2
0
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
2
0