Ruby-on-rails 如何从 Rails 控制台向数据库添加数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2489599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add data to database from rails console
提问by rails_guy
I have a Usermodel.
我有一个User模型。
>> @u = User.new
=> #<User id: nil, userid: nil, password: nil, created_at: nil, updated_at: nil, user_first_name: nil, user_last_name: nil, user_status: nil, user_type: nil>
I am not able to add data to the Userstable from the console. I am doing the following:
我无法Users从控制台向表中添加数据。我正在做以下事情:
>> @u.userid="test1"
=> "test1"
>> @u.password="test2"
=> "test2"
>> @u.user_first_name="test3"
=> "test3"
>> @u.user_last_name="test4"
=> "test4"
>> @u.user_status="test5"
=> "test5"
>> @u.user_type="test6"
=> "test6"
>> @u.save
NoMethodError: undefined method `userid' for nil:NilClass
what am i doing wrong? I just simply want to add one row of data to the app.
我究竟做错了什么?我只是想向应用程序添加一行数据。
回答by buru
>> u = User.create :userid => "myuserid", :password => "mypasswd", :user_first_name => "test", :user_last_name => "testovich", :user_status => "mystatus", :user_type => "mytype"
回答by Jits
Try checking to see if the instance you create is valid. The easiest way is to use the save! method...
尝试检查您创建的实例是否有效。最简单的方法是使用保存!方法...
@u.save!
This will cause an exception to be raised if the instance doesn't pass all validations, or if any other issues are found. The stack trace will then hopefully provide some useful info.
如果实例未通过所有验证或发现任何其他问题,这将导致引发异常。堆栈跟踪将有望提供一些有用的信息。
Also, instead of using @u = ...trying using just u = ...
此外,而不是使用@u = ...尝试只使用u = ...
回答by mliebelt
I have tried to create a new rails app, and I can do the following:
我试图创建一个新的 rails 应用程序,我可以执行以下操作:
irb(main):008:0> u= User.new
=> #<User id: nil, name: nil, created_at: nil, updated_at: nil>
irb(main):009:0> u.save
=> true
irb(main):011:0> User.find(3)
=> #<User id: 3, name: nil, created_at: "2010-03-22 11:51:31", updated_at: "2010-03-22 11:51:31">
The same works with create instead of new. I suppose that your model of User wants to have relation to another object which is not available yet. Could you provide your current scheme (located in db/schema.rb)?
同样适用于 create 而不是 new。我想您的 User 模型想要与另一个尚不可用的对象建立联系。您能否提供您当前的方案(位于 db/schema.rb 中)?
Mine is looking like that:
我的看起来像这样:
ActiveRecord::Schema.define(:version => 20100322114826) do
create_table "users", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
回答by bhavna garg
u = User.new(:userid => "myuserid", :password => "mypasswd", :user_first_name => "test", :user_last_name => "testovich", :user_status => "mystatus", :user_type => "mytype")
u.save
Simply by these 2 lines you can add data to your database.
只需通过这两行,您就可以将数据添加到数据库中。
回答by charlie.brown
You can enter this right from rails console: User.create(:userid => "myuserid", :password => "mypasswd", :user_first_name => "test", :user_last_name => "testovich", :user_status => "mystatus", :user_type => "mytype")
您可以从 rails 控制台输入此权限: User.create(:userid => "myuserid", :password => "mypasswd", :user_first_name => "test", :user_last_name => "testovich", :user_status => " mystatus", :user_type => "mytype")
I always like to run: .new so I can an exact list of what parameters I'm looking to add values to. Also not all the values need to be listed or filled out i.e. User.create(:userid => "myuserid", :password => "mypasswd") all other values will have a nil value. Hope this helps.
我总是喜欢运行: .new 这样我就可以得到我想要添加值的参数的确切列表。此外,并非所有值都需要列出或填写,即 User.create(:userid => "myuserid", :password => "mypasswd") 所有其他值都将具有 nil 值。希望这可以帮助。

