# Lots of duplicate row (except the id) in database

**URL:** <https://www.ruby-forum.com/t/lots-of-duplicate-row-except-the-id-in-database/127852>\
**Category:** Rails\
**Created:** [February 1, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/lots-of-duplicate-row-except-the-id-in-database/127852 "2008-02-01T21:11:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![semyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@semyou](https://www.ruby-forum.com/u/semyou)\
**Post date:** [February 1, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/lots-of-duplicate-row-except-the-id-in-database/127852/1 "2008-02-01T21:11:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![semyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@semyou](https://www.ruby-forum.com/u/semyou)\
**Post date:** [February 2, 2008, 12:11am UTC](https://www.ruby-forum.com/t/lots-of-duplicate-row-except-the-id-in-database/127852/2 "2008-02-02T00:11:39Z")

</div>

On 2/1/08, Youssef S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![semyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@semyou](https://www.ruby-forum.com/u/semyou)\
**Post date:** [February 2, 2008, 12:31am UTC](https://www.ruby-forum.com/t/lots-of-duplicate-row-except-the-id-in-database/127852/3 "2008-02-02T00:31:13Z")

</div>

Greg D. wrote:

> On 2/1/08, Youssef S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 D.  
> [http://destiney.com/](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

```

---

<div class="post-metadata">

**Author:** ![semyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ce7236/32.png) [@semyou](https://www.ruby-forum.com/u/semyou)\
**Post date:** [February 3, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/lots-of-duplicate-row-except-the-id-in-database/127852/4 "2008-02-03T22:47:49Z")

</div>

On Feb 1, 11:31 pm, Youssef S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> 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 [http://railscasts.com/episodes/73](http://railscasts.com/episodes/73) and the two following  
episodes to see how to handle this.

Allan
