lots of duplicate row (except the id) in database

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

No code = no clue.

Greg Donald 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

Hi,

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 #73 Complex Forms Part 1 - RailsCasts and the two following episodes to see how to handle this.

Allan