user1072337
user1072337

Reputation: 12945

Ruby on Rails - User not being entered into database

I have set up a Users model in RoR, and I am running into trouble when trying to enter in a new user in rails console. Here is the command and output I get:

[1] pry(main)> User.create(name: "Michael Hartl", email: "[email protected]", password: "foobar", password_confirmation: "foobar")

   (0.1ms)  BEGIN
  User Exists (0.2ms)  SELECT  1 AS one FROM `users`  WHERE `users`.`email` = '[email protected]' LIMIT 1
  SQL (0.2ms)  INSERT INTO `users` (`created_at`, `updated_at`) VALUES ('2014-08-18 22:53:03', '2014-08-18 22:53:03')
   (0.3ms)  COMMIT
=> #<User id: 4, name: nil, email: nil, created_at: "2014-08-18 22:53:03", updated_at: "2014-08-18 22:53:03", password_digest: nil>

It is saying User Exists (when it clearly doesn't already). Do you have any idea what's going on? Thanks.

EDIT:

User.all gives:

=> [#<User id: 1, name: "Bob Jones", email: "[email protected]", created_at: "2014-08-07 00:14:34", updated_at: "2014-08-07 00:14:34", password_digest: nil>,
 #<User id: 2, name: nil, email: nil, created_at: "2014-08-18 22:44:58", updated_at: "2014-08-18 22:44:58", password_digest: nil>,
 #<User id: 3, name: nil, email: nil, created_at: "2014-08-18 22:47:07", updated_at: "2014-08-18 22:47:07", password_digest: nil>,
 #<User id: 4, name: nil, email: nil, created_at: "2014-08-18 22:53:03", updated_at: "2014-08-18 22:53:03", password_digest: nil>]

The Bob Jones user I created was before this happened.

Edit 2:

User model (user.rb) looks like this:

class User < ActiveRecord::Base
    attr_accessor :name, :email, :password_digest
    before_save { self.email = email.downcase }
    validates :name, presence: true, length: { maximum: 50 }
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true,
                      format: { with: VALID_EMAIL_REGEX }, 
                      uniqueness: { case_sensitive: false }

    has_secure_password
    validates :password, length: { minimum: 6 }

    has_many :microposts
end

Migrations:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :email
      t.string :password_digest

      t.timestamps
    end
  end
end

class AddIndexToUsersEmail < ActiveRecord::Migration
  def change
    add_index :users, :email, unique: true
  end
end

class AddPasswordDigestToUsers < ActiveRecord::Migration
  def change
    add_column :users, :password_digest, :string
  end
end

EDIT 3:

Output using errors.full_messages (no message shows up), but it still doesn't put the user in (only a new entry will all null values)

pry(main)> User.create(name: "Michael Hartl", email: "[email protected]", password: "foobar", password_confirmation: "foobar").errors.full_messages
   (0.2ms)  BEGIN
  User Exists (0.2ms)  SELECT  1 AS one FROM `users`  WHERE `users`.`email` = '[email protected]' LIMIT 1
  SQL (0.2ms)  INSERT INTO `users` (`created_at`, `updated_at`) VALUES ('2014-08-19 00:33:40', '2014-08-19 00:33:40')
   (0.9ms)  COMMIT
=> []

Upvotes: 0

Views: 246

Answers (2)

Michał Młoźniak
Michał Młoźniak

Reputation: 5556

When you define ActiveRecord model, it has automatically generated setter and getter methods for all database columns. These methods just call read_attribute or write_attribute, which are responsible for reading and setting values from database records.

When you added attr_accessor to your model you overwrote active record methods. Your User class looked like this:

class User < ActiveRecord::Base
  # associations, validations etc.

  def name=(value)
    @name = value
  end

  def name
    @name
  end

  def email=(value)
    @email = value
  end

  def email
    @email
  end

  # and so on
end

After running User.create you had new record with @name and @email variables but no value in database since you've overwritten activerecord functionality.

You can read more about how activerecord stores values in record object in ActiveRecord::AttributeMethods::Write and ActiveRecord::AttributeMethods::Read classes.

Upvotes: 2

aznatam
aznatam

Reputation: 204

I had a similar problem, try deleting name and email from attr_accessor in your user model

Upvotes: 3

Related Questions