如何从 Rails 控制台向数据库添加数据 [英] how to add data to database from rails console

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

问题描述

我有一个 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>

我无法从控制台向 Users 表添加数据.我正在执行以下操作:

I am not able to add data to the Users table from the console. I am doing the following:

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

推荐答案

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

这篇关于如何从 Rails 控制台向数据库添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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