I have a model for users and a model for lists and a model for the items. I have a one-to-one relationship with the user and the list so only one list may be created. I''m having problems when I try to create the list at the same time I create the user so they are related by the foreign key on the list. How would I go about doing this. right now I''m trying def new @user = User.new @user.list.build end But I get an error message saying that list is nil. I know the problem must be with passing the user_id to the list model, but I have no idea how to do this. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
nek4life wrote:> I have a model for users and a model for lists and a model for the > items. > > I have a one-to-one relationship with the user and the list so only > one list may be created. > > I''m having problems when I try to create the list at the same time I > create the user so they are related by the foreign key on the list. > How would I go about doing this. > > right now I''m trying > > def new > @user = User.new > @user.list.build > end > > But I get an error message saying that list is nil. I know the > problem must be with passing the user_id to the list model, but I have > no idea how to do this.Try @user.build_list. If a user could have many lists, you''d use @user.lists.build. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 14, 3:06 pm, Jeremy Weiskotten <rails-mailing-l...@andreas- s.net> wrote:> nek4lifewrote: > > I have a model for users and a model for lists and a model for the > > items. > > > I have a one-to-one relationship with the user and the list so only > > one list may be created. > > > I''m having problems when I try to create the list at the same time I > > create the user so they are related by the foreign key on the list. > > How would I go about doing this. > > > right now I''m trying > > > def new > > @user = User.new > > @user.list.build > > end > > > But I get an error message saying that list is nil. I know the > > problem must be with passing the user_id to the list model, but I have > > no idea how to do this. > > Try @user.build_list. > > If a user could have many lists, you''d use @user.lists.build. > -- > Posted viahttp://www.ruby-forum.com/.Thanks Jeremy. I ended up using your solution in the create method after initializing the object in the new method. def new @user = User.new @list = List.new end def create @user = User.new(params[:user]) @user.build_list end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---