# Checking for duplicates, if duplicate, concate duplicate

**URL:** <https://www.ruby-forum.com/t/checking-for-duplicates-if-duplicate-concate-duplicate/129853>\
**Category:** Rails\
**Created:** [February 22, 2008, 2:26am UTC](https://www.ruby-forum.com/t/checking-for-duplicates-if-duplicate-concate-duplicate/129853 "2008-02-22T02:26:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [February 22, 2008, 2:26am UTC](https://www.ruby-forum.com/t/checking-for-duplicates-if-duplicate-concate-duplicate/129853/1 "2008-02-22T02:26:55Z")

</div>

Here’s the break down.

2 models: song, artist

artists has many songs.

Is there already a pre-built method in rails that can check for  
duplicate artists, and if the artist already exists, it will add the  
song object instead of creating a new artist?

If there isn’t I can write the method, would you write this in the model  
or the controller?

Basically it would do this.

grabs the artist[:name] params, do a find\_by\_name, if it finds it, then  
it will do concate with the song object, otherwise, it would create a  
new artist id.

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [February 22, 2008, 1:05pm UTC](https://www.ruby-forum.com/t/checking-for-duplicates-if-duplicate-concate-duplicate/129853/2 "2008-02-22T13:05:46Z")

</div>

Feng T.,

> grabs the artist[:name] params, do a find\_by\_name, if it finds it,  
> then  
> …, otherwise, it would create a new artist id.

try :  
a = Artist.find\_or\_create\_by\_name(params[:name])

Alain
