Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DangerousAttributeError in OmniAuth Railscast Tutorial: create is defined by ActiveRecord

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

like image 279
xta Avatar asked Apr 19 '12 03:04

xta


2 Answers

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.

like image 100
Ryan Yeske Avatar answered Nov 02 '22 18:11

Ryan Yeske


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

like image 40
Frederick Cheung Avatar answered Nov 02 '22 17:11

Frederick Cheung