Ike
(Ike)
October 7, 2009, 1:15pm
1
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
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
11175
(-- --)
October 7, 2009, 3:17pm
3
Greg Donald 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,
Mike27
(Mike)
October 7, 2009, 4:27pm
4
You could use find_or_create_by_note, too