Ruby on Rails w/ Devise - AssociationTypeMismatch User expected -


i'm new ror , i'm struggle creation of authorization instance belongs user. works fine when user doesn't exist, create user , it's first authorization. associationtypemismatch when tried create authorization existing user. idea ?

here's user class.

class user < activerecord::base   has_many :authorizations, :dependent => :destroy   devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable   attr_accessible :email, :password, :password_confirmation, :remember_me end 

my authorization class

class authorization < activerecord::base   attr_accessible :accesskey, :code, :provider_id, :refreshkey, :user_id, :authorization,     :user, :user_attributes     belongs_to :user end 

and method returns error on authorization.create

begin generatedauthorization   ...   user_email = s['email']   #check if user exists   user = user.where(:email => user_email)    if user.any?     flash[:notice] = 'old user'   else     password = 'uyflgmugmiuymmb'     user = user.create(:email => user_email, :password => password, :password_confirmation => password)   end   authorization.create(:provider_id => 1, :code => code, :refreshkey => refresh_token, :accesskey => access_token, :user => user) end 

replace user = user.where(:email => user_email) user = user.find_by_email(user_email)

then user.authorizations.create(:provider_id => 1, :code => code, :refreshkey => refresh_token, :accesskey => access_token)

#where returns relation object, allows other activerecord methods order, group chained. search email attribute, recommended use search helper find_by_email instead returns user object.
make work where: user = user.where(:email => user_email).first


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -