What am I missing here? Controller def add_history @history = History.new Patient.find(params[:id]).history.create(params[:history]) flash[:notice] = "Added History" redirect_to :action =>"show", :id => params[:id] end Models Patient has_one :history History belongs_to :patients when I try to add a History I get You have a nil object when you didn''t expect it! You might have expected an instance of ActiveRecord::Base. The error occured while evaluating nil.create --~--~---------~--~----~------------~-------~--~----~ 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 6/15/07, Armitage <rod.middleton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > What am I missing here? > > Controller > def add_history > @history = History.new > Patient.find(params[:id]).history.create(params[:history]) > flash[:notice] = "Added History" > redirect_to :action =>"show", :id => params[:id] > end > > Models > Patient > has_one :history > History > belongs_to :patients > > when I try to add a History I get > > You have a nil object when you didn''t expect it! > You might have expected an instance of ActiveRecord::Base. > The error occured while evaluating nil.createI believe your thinking that a has_one assocaition has a create method like a has_many. The has_one association works a bit different. Your patient will have a create_history method instead. You could rewrite this as from def add_history @history = History.new # Does nothing You don''t re-assign it. Patient.find(params[:id]).history.create( params[:history])> flash[:notice] = "Added History" > redirect_to :action =>"show", :id => params[:id] > endto def add_history @history = Patient.find( params[:id] ).create_history( params[:history] ) || History.new flash[:notice] = "Added History" redirect_to :action => "show", :id => params[:id] end This will effectivley give the same functionality as you have. You may want to add a bit of a rescue clause in the case that the Patient is not found. Or use find_by_id which doesn''t throw an Exception immediatley, but will when you call create_history on nil. Hope that helps Daniel --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thats perfect! Thank you very much indeed On Jun 15, 10:22 am, "Daniel N" <has....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 6/15/07, Armitage <rod.middle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > What am I missing here? > > > Controller > > def add_history > > @history = History.new > > Patient.find(params[:id]).history.create(params[:history]) > > flash[:notice] = "Added History" > > redirect_to :action =>"show", :id => params[:id] > > end > > > Models > > Patient > > has_one :history > > History > > belongs_to :patients > > > when I try to add a History I get > > > You have a nil object when you didn''t expect it! > > You might have expected an instance of ActiveRecord::Base. > > The error occured while evaluating nil.create > > I believe your thinking that a has_one assocaition has a create method like > a has_many. The has_one association works a bit different. Your patient > will have a create_history method instead. You could rewrite this as > > from > def add_history > @history = History.new # Does nothing You don''t re-assign it. > Patient.find(params[:id]).history.create( params[:history]) > > > flash[:notice] = "Added History" > > redirect_to :action =>"show", :id => params[:id] > > end > > to > > def add_history > @history = Patient.find( params[:id] ).create_history( params[:history] ) > || History.new > flash[:notice] = "Added History" > redirect_to :action => "show", :id => params[:id] > end > > This will effectivley give the same functionality as you have. > > You may want to add a bit of a rescue clause in the case that the Patient is > not found. Or use find_by_id which doesn''t throw an Exception immediatley, > but will when you call create_history on nil. > > Hope that helps > Daniel--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---