如何从rails控制台向数据库添加数据

时间:2022-08-07 15:16:10

I have a User model.

我有一个用户模型。

>> @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 Users table 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.

我做错了什么?我只是想在app中添加一行数据。

5 个解决方案

#1


49  

>> u = User.create :userid => "myuserid", :password => "mypasswd", :user_first_name => "test", :user_last_name => "testovich", :user_status => "mystatus", :user_type => "mytype"

#2


17  

Try checking to see if the instance you create is valid. The easiest way is to use the save! method...

尝试检查创建的实例是否有效。最简单的方法是使用save!方法……

@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 =…

#3


2  

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。我想您的用户模型想要与另一个尚未可用的对象建立关系。你能提供你目前的计划(位于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

#4


0  

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.

通过这两行,您可以向数据库中添加数据。

#5


0  

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 =>“vich”,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。创建(:userid => " usermyid ",:password => "mypasswd")所有其他值都有nil值。希望这个有帮助。

#1


49  

>> u = User.create :userid => "myuserid", :password => "mypasswd", :user_first_name => "test", :user_last_name => "testovich", :user_status => "mystatus", :user_type => "mytype"

#2


17  

Try checking to see if the instance you create is valid. The easiest way is to use the save! method...

尝试检查创建的实例是否有效。最简单的方法是使用save!方法……

@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 =…

#3


2  

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。我想您的用户模型想要与另一个尚未可用的对象建立关系。你能提供你目前的计划(位于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

#4


0  

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.

通过这两行,您可以向数据库中添加数据。

#5


0  

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 =>“vich”,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。创建(:userid => " usermyid ",:password => "mypasswd")所有其他值都有nil值。希望这个有帮助。