DangerousAttributeError in OmniAuth Railscast Tutorial: create is defined by ActiveRecord

只愿长相守 提交于 2019-12-01 16:09:27

Activerecord is warning you that some of your database attribute names (create etc.) clash with the names of instance methods provided by activerecord/ruby.

Since rails would otherwise create instance methods of those names to access attributes, such a clash used to cause really weird things to happen. Thus active record raises an exception to warn you that this is happening

I just ran into this following the same RailsCast.

The tutorial says to run:

rails g nifty:scaffold authentication user_id:integer \
        provider:string uid:string index create destroy

But not having the nifty scaffold stuff on my machine, I just ran

rails g scaffold authentication user_id:integer \
        provider:string uid:string index create destroy

Which behaves differently. Instead of creating stub 'index', 'create', and 'destroy' controller methods, it created fields in the database.

Remove them and it works fine, as mentioned previously.

So just to finish the question off you will need to create a migration using this command:

rails g migration remove_silly_authentication_fields_which_should_not_be_there

Which looks something like this:

class DropSillyControllerAttributes < ActiveRecord::Migration
   def change
      remove_column :authentications, :index
      remove_column :authentications, :create
      remove_column :authentications, :destroy
   end
end

And run it using the usual:

rake db:migration

Or alternatively you should be able to run:

rake db:rollback

To roll back the changes just made to the database and:

rails d scaffold authentication

To remove all the files, then run:

rails g scaffold authentication user_id:integer provider:string uid:string

And do the other stuff manually

I did exactly the same thing myself by the way.

Try: current_user.authentications.create!

EDIT

So basically your problem was that you had columns in your table named the same as methods of the Modal class.

You can't have a column named create or destroy in your database.

Most likely it was a typo on your model/controller generation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!