That is why constraints should be handled by databases in my opinion.
Just create the unique index in the database and write something like this:
begin
company.save!
rescue UniqueConstraintException => e # I don't really know the name for
this exception
render json: {error_message: "Please remove the duplicate entries."}
rescue e # general rescuer
log.error "Couldn't create company", e # not sure if that is the
right syntax
render json: {error_message: "We could't process your request. Please
contact our support team."}
end
Well, at least this has always been my opinion and the reason why I
prefer writing validations in the database using unique constraints or
triggers.
Usually the ActiveRecord and Hibernate communities among others don't
recommend this approach if you intend to support multiple databases, but
since I always use PostgreSQL, this is not an issue to me. That is one
of the reasons I prefer Sequel over AR. The Sequel community will
usually prefer handling validations in the database-level even though
you can use validators in Ruby as well to make it easy to handle simple
cases but you should always replicate the validations in the database
level as well.
Well, that is my opinion since you asked :)
Cheers,
Rodrigo.
Em 05-08-2012 11:36, Gabriel Sobrinho escreveu:
Anyone have some opinion?
On Sunday, July 29, 2012 4:17:01 PM UTC-3, Gabriel Sobrinho wrote:
Hi,
Currently uniqueness validator uses database queries which not
work for nested forms.
For example, I have a company form which have many partners and
each partner have a person and a percentage.
Using uniqueness validator, I can not guarantee someone will not
be a partner two times because the partners aren't on database yet.
We've implemented a validator similar to this one:
http://pastie.org/private/osa3pmono5l1ykrd7vipwa
<http://pastie.org/private/osa3pmono5l1ykrd7vipwa>
I'm not sure if the uniqueness validator should handle it.
Take a look: http://pastie.org/private/maxdpn4gmvftzcl2ka0mq
<http://pastie.org/private/maxdpn4gmvftzcl2ka0mq>
I'm not sure if it will be too complex to the proposal of
uniqueness validator but that is a common validation for each
system I've worked in last years.
WDYT? Something like that should be on the active record or a gem?
Cheers,
Gabriel Sobrinho
gabrielsobrinho.com <http://gabrielsobrinho.com>
--
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.