Hi, I have a 2 tables project and task. The task table has a foreign key on projects. Whenever, I create a new task through my view, I see my added task and a new project_id, but my project table keeps on having duplicate rows for the project, as long as I keep on submitting tasks for the same project. I thougth that Rails would handle this by itself? Any idea? Suggestions? Thanks, Youssef -- 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 2/1/08, Youssef Semlani <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have a 2 tables project and task. The task table has a foreign key on > projects. Whenever, I create a new task through my view, I see my added > task and a new project_id, but my project table keeps on having > duplicate rows for the project, as long as I keep on submitting tasks > for the same project. I thougth that Rails would handle this by itself? > Any idea? Suggestions?No code = no clue. -- Greg Donald http://destiney.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 -~----------~----~----~----~------~----~------~--~---
Youssef Semlani
2008-Feb-01 23:31 UTC
Re: lots of duplicate row (except the id) in database
Greg Donald wrote:> On 2/1/08, Youssef Semlani <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> I have a 2 tables project and task. The task table has a foreign key on >> projects. Whenever, I create a new task through my view, I see my added >> task and a new project_id, but my project table keeps on having >> duplicate rows for the project, as long as I keep on submitting tasks >> for the same project. I thougth that Rails would handle this by itself? >> Any idea? Suggestions? > > No code = no clue. > > > -- > Greg Donald > http://destiney.com/Hi Greg, Here is my code def create @project = Project.new(params[:project]) @task = @project.tasks.build(params[:task]) if @project.save && @task.save flash[:notice] = ''Entry was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end def new @project = Project.new @task = Task.new 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 -~----------~----~----~----~------~----~------~--~---
On Feb 1, 11:31 pm, Youssef Semlani <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Here is my code > > def create > @project = Project.new(params[:project]) > @task = @project.tasks.build(params[:task]) > > if @project.save && @task.save > flash[:notice] = ''Entry was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end > > def new > @project = Project.new > @task = Task.new > > endHi, maybe you need to search the database to see if there''s an existing project entry already and update that one rather than creating one from scratch each time? So in create, do something like: @project = Project.find_by_name(params[:project][:name]) Check out http://railscasts.com/episodes/73 and the two following episodes to see how to handle this. Allan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---