I have 3 models . Doctor, Patient and User with following associations class Doctor < ActiveRecord::Base has_many :patients, :dependent => :destroy has_one :user, :as => :userable accepts_nested_attributes_for :user end class Patient < ActiveRecord::Base belongs_to :doctor has_one :user, :as => :userable accepts_nested_attributes_for :user end class User < ActiveRecord::Base belongs_to :userable, :polymorphic => true end I have a nested form for doctor and user as OUTER FORM for :doctor INNER FORM for :user end end and in my users controller i have a create action as def create doc_attr = params[:doctor] user_attr = doc_attr.delete(:user) doctor = Doctor.new(doc_attr) doctor.save! doctor.user.create(user_attr) ### getting error on this .................... end When i submit the form i get the error saying undefined method create for nil:NilClass I know this is relatively simple. Any help will be greatly appreciated. Thanks in advance !!! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ok got this working. :) On Oct 7, 3:57 pm, Dean <deansam2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have 3 models . Doctor, Patient and User > with following associations > > class Doctor < ActiveRecord::Base > has_many :patients, :dependent => :destroy > has_one :user, :as => :userable > accepts_nested_attributes_for :user > end > > class Patient < ActiveRecord::Base > belongs_to :doctor > has_one :user, :as => :userable > accepts_nested_attributes_for :user > end > > class User < ActiveRecord::Base > belongs_to :userable, :polymorphic => true > end > > I have a nested form for doctor and user as > OUTER FORM for :doctor > INNER FORM for :user > end > end > > and in my users controller i have a create action as > > def create > doc_attr = params[:doctor] > user_attr = doc_attr.delete(:user) > doctor = Doctor.new(doc_attr) > doctor.save! > doctor.user.create(user_attr) ### getting error on this > .................... > end > > When i submit the form i get the error saying undefined method create > for nil:NilClass > I know this is relatively simple. Any help will be greatly > appreciated. Thanks in advance !!!-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Maybe Matching Threads
- How to use group in nested associations
- Rails 3 save parent model and association if nested attributes validation fails for uniqueness
- accepts_nested_attributes_for :reject_if issue
- Question about PATCH method, accepts_nested_attributes_for, and updates to association lists (has_many, HABTM)
- how to use activemodel collection.build for a has_many :through association