Ruby on Rails - 用户未输入数据库

时间:2014-08-18 23:27:32

标签: ruby-on-rails ruby

我在RoR中设置了一个用户模型,在尝试在rails控制台中输入新用户时遇到了麻烦。这是我得到的命令和输出:

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

   (0.1ms)  BEGIN
  User Exists (0.2ms)  SELECT  1 AS one FROM `users`  WHERE `users`.`email` = 'mhartl@example.com' 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>

它表示用户存在(当它显然尚未存在时)。你知道发生了什么吗?谢谢。

编辑:

User.all给出:

=> [#<User id: 1, name: "Bob Jones", email: "jones@gmail.com", 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>]

我创建的Bob Jones用户就是在此之前发生的。

编辑2:

用户模型(user.rb)如下所示:

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

迁移:

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

编辑3:

使用errors.full_messages输出(没有消息显示),但它仍然没有将用户放入(只有一个新条目将全部为空值)

pry(main)> User.create(name: "Michael Hartl", email: "mhartl@example.com", password: "foobar", password_confirmation: "foobar").errors.full_messages
   (0.2ms)  BEGIN
  User Exists (0.2ms)  SELECT  1 AS one FROM `users`  WHERE `users`.`email` = 'mhartl@example.com' 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
=> []

2 个答案:

答案 0 :(得分:3)

我遇到了类似的问题,请尝试在用户模型中删除attr_accessor的姓名和电子邮件

答案 1 :(得分:2)

定义ActiveRecord模型时,它会自动为所有数据库列生成setter和getter方法。这些方法只调用read_attribute或write_attribute,它们负责从数据库记录中读取和设置值。

当您将attr_accessor添加到模型时,您会覆盖活动记录方法。您的用户类看起来像这样:

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

运行User.create后,你有了@name和@email变量的新记录,但由于你已经覆盖了activerecord功能,所以数据库中没有值。

您可以在ActiveRecord :: AttributeMethods :: Write和ActiveRecord :: AttributeMethods :: Read类中阅读有关activerecord如何在记录对象中存储值的更多信息。