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
On Wed, Oct 7, 2009 at 8:15 AM, RVince <rvince99-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> > > 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.savebegin 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 Donald http://destiney.com/
Greg Donald wrote:> On Wed, Oct 7, 2009 at 8:15 AM, RVince <rvince99-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> 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 cnOr put a validates_uniqueness_of in the app.> > > -- > Greg Donald > http://destiney.com/Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
You could use find_or_create_by_note, too 2009/10/7, Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Greg Donald wrote: >> On Wed, Oct 7, 2009 at 8:15 AM, RVince <rvince99-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> 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 Donald >> http://destiney.com/ > > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > > >-- Von meinen Mobilgerät aus gesendet