Question

I've run into an error in the Rails console. I'm using the latest version of Rails and Pry. I am attempting to generate a user. I am able to set values within user attributes, but am unable to save them. Maybe User.new is looping somewhere? Here's the attribute-related code of my model:

require 'bcrypt'
class User < ActiveRecord::Base

attr_accessible :first_name, :last_name, :description, :profile_photo, :password, :password_confirmation, :email
attr_reader :password

has_secure_password

validates_presence_of :email, :session_token, :first_name, :last_name, :password_digest
validates_uniqueness_of :email
validates :password, length: { minimum: 6, maximum: 20 } 

before_validation :reset_session_token!, on: :create
before_save :encrypt_new_password

This is my users migration:

class CreateUsers < ActiveRecord::Migration
def change
 create_table :users do |t|
  t.string  :email, unique: true, null: false
  t.string  :password_digest, null: false
  t.string  :first_name, null: false
  t.string  :last_name, null: false
  t.string  :description, limit: 400
  t.string  :session_token, null: false
  t.attachment :profile_photo
  t.integer :profile_photo_id
  t.datetime :profile_photo_updated_at

  t.timestamps
end

  add_index :users, :email
  add_index :users, :first_name
  add_index :users, :last_name
  add_index :users, :session_token
  add_index :users, :created_at
 end
end

And here's my Rails console error after trying to create a user via User.new(values) then .save, or User.create(values), or u = User.new then u.value = value then u.save.

[10] pry(User):1> u.save
(0.2ms)  BEGIN
(0.2ms)  ROLLBACK
SystemStackError: stack level too deep
from /Users/joecase/.rvm/gems/ruby-2.1.0/gems/pry-0.9.12.6/lib/pry/pry_instance.rb:328
[11] pry(User):1> u.errors
=> #<ActiveModel::Errors:0x0000010527ccb0
@base=
#<User id: nil, email: "joecase@nyu.edu", password_digest: "111111", first_name: "Joe", last_name: "Case", description: nil, session_token: "CVnrsyyTwoOp9TZkqmVfTw", profile_photo_file_name: nil, profile_photo_content_type: nil, profile_photo_file_size: nil, profile_photo_updated_at: nil, profile_photo_id: nil, created_at: nil, updated_at: nil>,
@messages={}>

[12] pry(User):1> u.errors.full_messages
=> []

ANY help would be appreciated. Thank you in advance.

encrypt_new_password:

def encrypt_new_password
  return if password.blank?
  self.hashed_password = encrypt(password)
end

reset_session_token:

 def reset_session_token
   self.session_token ||= SecureRandom.urlsafe_base64(16)
   save!
 end
Était-ce utile?

La solution

Most likely, the problem would be in the the call back function before_save :encrypt_new_password.

If the function :encrypt_new_password tries to update and save the user record you will face this problem, would be great if you can edit your question and post the :encrypt_new_password function as it is the function most likely causing the infinite callbacks.

Looking at the two provided functions for the callbacks, the problem is in the function of the callback for the validation.

def reset_session_token
   self.session_token ||= SecureRandom.urlsafe_base64(16)
   save!
end

before validations you save which would validate and before validations you save and so on. So try to hash out the save! call it should be saved, if not try to make it an after_validation callback.

Autres conseils

Maybe there is some confusion that you are calling in callback reset_session_token! but defined method without a excalmation mark? No other idea though.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top