Rails控制台添加nil而不是值 [英] Rails console is adding nil instead of values

查看:84
本文介绍了Rails控制台添加nil而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,当尝试通过Rails控制台或种子数据将新用户添加到我的User表时,一切都为零。

Currently when trying to add a new user to my User table via rails console or seed data, everything is nil.

在我的种子文件中,我正在运行:

In my seed file I am running:

u1 = User.create(:from_email => "v@gmail.com", :to_email => "s@gmail.com", :from_name=>"Vero", :to_name=>"Spencer", :message=>"Test message", :password=>"chicken", :id => 1)
u1.save!

如果我通过rails中的种子数据或通过rails控制台运行它,然后使用User检查Postgres数据库.all我得到:

If I run this via seed data in rails or via rails console and then check the Postgres database with User.all I get:

#<User id: 1, from_name: nil, to_name: nil, from_email: nil, to_email: nil, message: nil, created_at: "2014-12-10 02:34:02", updated_at: "2014-12-10 02:34:02", password: nil>

我已经处理了好几个小时,开始变得很困惑。我们将提供帮助,我们的模型和架​​构如下:

I've been dealing with this for hours, starting to get quite confused. Help would be appreciated, my model and schema is below:

class User < ActiveRecord::Base
attr_accessor :from_name, :from_email, :to_name, :to_email, :message, :password, :id

before_save { self.from_email = from_email.downcase }
before_save { self.to_email = to_email.downcase }
before_save { self.password = "merrychristmas#{ Random.rand(1000) }" }

validates :id, :presence => true, :uniqueness => true
validates :from_name, :presence => true, :uniqueness => false
validates :to_name, presence: true, :uniqueness => false
validates :from_email, presence: true, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i}
validates :to_email, presence: true, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }
validates :message, presence: true, length: { maximum: 50 }

def self.authenticate(id, password)
    user = find(id)
if user && user.password
        user
else
        nil
end
end

end

模式:

ActiveRecord::Schema.define(version: 20141209104326) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "users", force: true do |t|
    t.string   "from_name"
    t.string   "to_name"
    t.string   "from_email"
    t.string   "to_email"
    t.text     "message"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "password"
  end

end

SQL

(0.3ms)  BEGIN
  User Exists (0.5ms)  SELECT  1 AS one FROM "users"  WHERE "users"."id" = 2 LIMIT 1
  SQL (0.4ms)  INSERT INTO "users" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", "2014-12-10 04:15:33.579478"], ["updated_at", "2014-12-10 04:15:33.579478"]]
   (6.5ms)  COMMIT
=> #<User id: nil, from_name: nil, to_name: nil, from_email: nil, to_email: nil, message: nil, created_at: "2014-12-10 04:15:33", updated_at: "2014-12-10 04:15:33", password: nil>


推荐答案

您的问题正在使用 attr_accessor 在您的模型中。仅应在要为不想存储在数据库中的对象定义getter / setter方法的地方使用此方法。 请参阅此处以获取更多信息。

Your problem is using attr_accessor in your model. This should only be used where you want to define getter/setter methods for an object which you do not want to store in the database. See here for more information.

删除整个以 attr_accessor 开头的行,您的代码将可用。

Remove the entirety of the line that starts attr_accessor and your code will work.

也删除 id 的赋值以及验证其存在的行。这是由数据库自动处理的。

Also remove the assignment of the id and the line that validates its presence. This is handled automatically by the database.

这篇关于Rails控制台添加nil而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆