# Unique indexes

**URL:** <https://www.ruby-forum.com/t/unique-indexes/176881>\
**Category:** Rails\
**Created:** [October 7, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/unique-indexes/176881 "2009-10-07T15:16:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [October 7, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/unique-indexes/176881/1 "2009-10-07T15:16:39Z")

</div>

I have a unique index on Channelnote.note.  
In my controller, I go to save the Channelnote.  
But, if the note portion already exists, it naturally throws an error.  
How can I alleviate this, so that it only saves if the note is NOT  
already in the table? Thanks, RVince

```
    cnote = Channelnote.new

```

cnote.note = params[‘channelnotes’]  
cnote.channel\_id = @channel.id  
cnote.associate\_id = current\_associate.id  
g=cnote.save

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [October 7, 2009, 3:50pm UTC](https://www.ruby-forum.com/t/unique-indexes/176881/2 "2009-10-07T15:50:01Z")

</div>

On Wed, Oct 7, 2009 at 8:15 AM, RVince [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> cnote.channel_id = @channel.id
> cnote.associate_id = current_associate.id
> g=cnote.save
> 
> ```

begin  
cnote.save!  
rescue  
flash[:notice] = ‘channel note failed to save’  
end

or you could check to see if it exists before trying to save it.

cn = Channelnote.find\_by\_note( params[:channelnotes] )  
cnote.save unless cn

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [October 7, 2009, 5:17pm UTC](https://www.ruby-forum.com/t/unique-indexes/176881/3 "2009-10-07T17:17:04Z")

</div>

Greg D. wrote:

> On Wed, Oct 7, 2009 at 8:15 AM, RVince [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ï¿½ ï¿½ ï¿½ ï¿½cnote.channel\_id = @channel.id  
> > ï¿½ ï¿½ ï¿½ ï¿½cnote.associate\_id = current\_associate.id  
> > ï¿½ ï¿½ ï¿½ ï¿½g=cnote.save
> 
> begin  
> cnote.save!  
> rescue  
> flash[:notice] = ‘channel note failed to save’  
> end
> 
> or you could check to see if it exists before trying to save it.
> 
> cn = Channelnote.find\_by\_note( params[:channelnotes] )  
> cnote.save unless cn

Or put a validates\_uniqueness\_of in the app.

> –  
> Greg D.  
> [http://destiney.com/](http://destiney.com/)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![RVince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@RVince](https://www.ruby-forum.com/u/RVince)\
**Post date:** [October 7, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/unique-indexes/176881/4 "2009-10-07T18:29:18Z")

</div>

You could use find\_or\_create\_by\_note, too

2009/10/7, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > flash[:notice] = ‘channel note failed to save’
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Von meinen MobilgerÃ¤t aus gesendet
