8

I've looked at ActiveRecord::DangerousAttributeError and other similar threads on SO, but they don't address the same issue.

I'm following the omniauth tutorial: http://railscasts.com/episodes/235-omniauth-part-1?view=asciicast

I'm able to authenticate via oauth with Twitter and return the user's data (auth). The problem is that I'm not able to create/save it in the database (sqlite3) because of this error message.

Error:

ActiveRecord::DangerousAttributeError in AuthenticationsController#create

create is defined by ActiveRecord
Rails.root: /beta/devise-omniauth1

Application Trace | Framework Trace | Full Trace
app/controllers/authentications_controller.rb:15:in `create'

Authentications_Controller:

  def create
    auth = request.env["omniauth.auth"] 
    current_user.authentications.create(:provider => auth['provider'], :uid => auth['uid'])
    flash[:notice] = "Authentication successful."
    redirect_to authentications_url
  end

Models:

class Authentication < ActiveRecord::Base
belongs_to :user
end


class User < ActiveRecord::Base
has_many :authentications

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and     :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
end

How do I work past this error? Googling on this site and others doesn't help me understand what's going on in order to fix it. Thanks

Community
  • 1
  • 1
xta
  • 729
  • 2
  • 8
  • 29
  • What columns do the user and authentication models have – Frederick Cheung Apr 19 '12 at 05:27
  • authentication: user_id, provider, uid, index, create, destroy Thanks, i updated the migration and the error is resolved after taking the last 3 columns out of the table – xta Apr 19 '12 at 07:25
  • @XTA i have the exact same problem, in what way did you remove the last 3 columns out of the table? I'm sort of a newb =) – Apane101 Feb 28 '13 at 19:26

4 Answers4

8

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.

Ryan Yeske
  • 456
  • 2
  • 4
  • I just tan into the same problem-What is the best way to go about removing those fields? –  Feb 28 '13 at 08:05
4

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

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
4

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.

rogermushroom
  • 5,486
  • 4
  • 42
  • 68
  • thanks for that! I have just ran into the same issue, although can you please clear up what exactly you put for: "rails g migration remove_silly_authentication_fields_which_should_not_be_there" – Apane101 Feb 28 '13 at 19:37
  • @Apane101 This should be name of the migration you created which was incorrect. – rogermushroom Apr 26 '13 at 17:46
1

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.

Eric Sites
  • 1,494
  • 10
  • 16
  • After using `current_user.authentications.create!(:provider => auth['provider'], :uid => auth['uid'])`, I still have the error: `ActiveRecord::DangerousAttributeError in AuthenticationsController#create create is defined by ActiveRecord` – xta Apr 19 '12 at 07:18