adding hobby to a user
Posted
by
rookieRailer
on Stack Overflow
See other posts from Stack Overflow
or by rookieRailer
Published on 2011-01-03T05:45:00Z
Indexed on
2011/01/03
5:54 UTC
Read the original article
Hit count: 290
ruby-on-rails
I'm trying to write a rails application. I'm a ruby-on-rails newbie. I have a User model and a Hobby model.
class User < ActiveRecord::Base
has_many :hobbies
end
class Hobby < ActiveRecord::Base
belongs_to :user
end
During the new user registration, I have used a text box where I enter a value for hobby. Then, when I press a button 'Add hobby', a method in UsersController add_hobby is to be called where I intend to append the value entered for hobby by user to the user i.e
@user.hobbies << hobby
However, my problem is that the user object has not been saved yet, so there is no way to access a particular user object. How do I get around this problem ?
© Stack Overflow or respective owner