Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add data to database from rails console

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:

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

like image 743
rails_guy Avatar asked Mar 22 '10 01:03

rails_guy


People also ask

How do I access rails console?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .

What is database in Ruby on Rails?

Rails comes with built-in support for SQLite, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.


1 Answers

>> u = User.create :userid => "myuserid", :password => "mypasswd", :user_first_name => "test", :user_last_name => "testovich", :user_status => "mystatus", :user_type => "mytype" 
like image 136
buru Avatar answered Sep 17 '22 23:09

buru