Hi, I have a form for two models, a Requirement has_many Posts @requirement = Requirement.new(params[:requirement]) @post = @requirement.posts.build(params[:post]) That''s what I have so far to save the requirement. I can save the requirement fine. However, if the user inputs a title for a requirement that already exists, I want to check that, and add the user''s post to THAT requirement (that already exists). How would I do this? Requirement has only one column ''title''... I''ve tried @requirements = Requirement.find(:all) @requirements.each do |req| if(params[:requirement].include?(req.title)) curr_req = req end end if curr_req @post = curr_req.posts.build(params[:post]) if @post.save redirect_to :action => :index else redirect_to :action => :new_requirement end else @requirement = Requirement.new(params[:requirement]) @post = @requirement.posts.build(params[:post]) if @post.save redirect_to :action => :index else redirect_to :action => :new_requirement end end Doesn''t work... I also received an error: Unknown key(s): title Any help is much appreciated, thanks! -- 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 Jul 18, 2:43 am, Justin To <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, I have a form for two models, a Requirement has_many Posts > > @requirement = Requirement.new(params[:requirement]) > @post = @requirement.posts.build(params[:post]) > > That''s what I have so far to save the requirement. I can save the > requirement fine. However, if the user inputs a title for a requirement > that already exists, I want to check that, and add the user''s post to > THAT requirement (that already exists). How would I do this? Requirement > has only one column ''title''... > > I''ve tried > > @requirements = Requirement.find(:all) > > @requirements.each do |req| > if(params[:requirement].include?(req.title)) > curr_req = req > end > end >That would work if you had instead said params[:requirement][:title] == req.title (not sure what you were thinking would happen with include?) This is an incredibly inefficient way of doing things when you could just do curr_req = Requirement.find_by_title params[:requirement] [:title]. Fred> if curr_req > @post = curr_req.posts.build(params[:post]) > if @post.save > redirect_to :action => :index > else > redirect_to :action => :new_requirement > end > else > @requirement = Requirement.new(params[:requirement]) > @post = @requirement.posts.build(params[:post]) > > if @post.save > redirect_to :action => :index > else > redirect_to :action => :new_requirement > end > end > > Doesn''t work... I also received an error: Unknown key(s): title > > Any help is much appreciated, thanks! > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> That would work if you had instead said params[:requirement][:title] > == req.title (not sure what you were thinking would happen with > include?) > This is an incredibly inefficient way of doing things when you could > just do curr_req = Requirement.find_by_title params[:requirement] > [:title]. > > FredThanks for your continued help Fred, really appreciate it. Pretty new to this stuff, so I''m still trying to learn. Justin -- 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 -~----------~----~----~----~------~----~------~--~---
Justin To wrote:> >> That would work if you had instead said params[:requirement][:title] >> == req.title (not sure what you were thinking would happen with >> include?) >> This is an incredibly inefficient way of doing things when you could >> just do curr_req = Requirement.find_by_title params[:requirement] >> [:title]. >> >> Fred > > Thanks for your continued help Fred, really appreciate it. > > Pretty new to this stuff, so I''m still trying to learn. > > Justinwhy not class requirement validates_uniqueness_of :title end -- 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 -~----------~----~----~----~------~----~------~--~---