[Rails] Re: rescuing ActiveRecord::RecordNotUnique: clever or ugly?
Joe V. wrote in post #1053641:
> A SQL query takes around 1 ms, possibly more if the db server is on a
> different machine. That is way more expensive than rescuing an
> exception.
Joe: Thanks for the useful data point. Just to make sure my
understanding is complete, since any db transaction is going to take 1ms
(more or less), you would ALWAYS let the db catch the duplicate and
avoid the extra SELECT:
def self.create_relation(src, dst)
create!(:src_id => src.id, :dst_id => dst.id)
rescue ActiveRecord::RecordNotUnique
where(:src_id => src.id, :dst_id => dst.id)
end
Yes?
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home