0

I am try on to insert a value via the Rails console into the database, but it's not working.

the first command is
u=users.create(:name=>"bob",:address=>"Dublin")

this is the output after I running the first command

 u=Users.create(:name=>"Ben",:address=>"Dublin")
   (0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `users` (`address`, `created_at`, `email`, `name`, `password`, `updated_at`) VALUES ('Dublin', '2012-04-16 23:15:48', NULL, 'Ben', NULL, '2012-04-16 23:15:48')
   (9.1ms)  COMMIT
=> #<Users id: 2, name: "Ben", password: nil, email: nil, address: "Dublin", created_at: "2012-04-16 23:15:48", updated_at: "2012-04-16 23:15:48">

this is the second command
t=tweets.create(:status=>"I am a tweet from bob",:user=>u)

NameError: undefined local variable or method `tweets' for main:Object
    from (irb):4
    from /opt/bitnami/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
    from /opt/bitnami/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
    from /opt/bitnami/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
3
  • tweets should be Tweet. Capital because its a class name, in ruby class names starts with Capital letter. If its relation then User.find(id).tweets.create(:status => "bla bla") but in this case you don't need :user => because he will know to who attach it. By Magic Commented Apr 16, 2012 at 23:13
  • Hello,thanks for help,but I am to create a valeu in tweets table which reference in users table? Commented Apr 16, 2012 at 23:38
  • which is user_id,so I mean each tweet has a user_id. Commented Apr 16, 2012 at 23:38

1 Answer 1

1

Assuming you have a Tweet model, you want

u = User.first
t = Tweet.create(:status => "I am a tweet from bob", :user => u)
Sign up to request clarification or add additional context in comments.

1 Comment

What? Are you saying that the above doesn't work either? What's the error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.