Getting Acts_as_Paranoid to work with validates_uniqueness_o


#1

Hello
I’ve been using Acts_as_Parnoid for a while and it has been working well
but I am now having a slight issue with it. I have a company table were
I would like to use validates_uniqueness_of :company_name. This
validation works if a company exists in the database and has not been
deleted. However, I also would like the validates_uniqueness_of to
work if a company has previously been deleted and has been flagged in
the deleted_at column. Currently, if a company has been deleted, it is
possible to create another company with the same name. I would like to
bring up a validation message to inform the user. Is there an easy way
to accomplish this?

Any help would be greatly appreciated.

Harvey


#2

Harvey B. wrote:

to accomplish this?

Any help would be greatly appreciated.

Two questions:

  1. If you want to create a company with the same name as something
    already deleted, is it OK to resurrect the old company and use that
    record?
  2. If not, then perhaps you can have two companies with the same
    name–so validates_uniqueness_of would not be correct?

Basically you have to decide what you want to do with the conflict. If
a company exists with the name that you want to use but it’s status is
deleted, you can either reuse the company (reinstating it), or you will
have to reject the new company because its name is not unique. The only
way out is to allow two companies with the same name. You’d be
surprised how many companies in Northern Virginia contract the
abbreviations for the area in there name: NoVa. Most of them only
differentiate by adding a little extra to the name like Nova Community
College, Nova Medical Center, Nova Contracting, etc.

I’d say that both validates_uniqueness_of and acts_as_paranoid are doing
their jobs correctly.


#3

Berin L. wrote:

company with the same name. I would like to bring up a validation

  1. If not, then perhaps you can have two companies with the same
    College, Nova Medical Center, Nova Contracting, etc.
    Ok, never mind, I should reread the message again before sending…

You want it to behave properly and it isn’t throwing the validation you
want. Got it.


#4

Berin
Thanks for your help. I was thinking about writing overriding the
validate method and testing for the condition myself. I’m not entirely
sure if this is the best way to proceed? So any help would be greatly
appreciatd
Thank you

Regards
Harvey


#5

Harvey B. wrote:

to accomplish this?

Any help would be greatly appreciated.

Looking at the plugin code, you would have to specify the option
:with_deleted => true to find companies that have been deleted. The
full list of options for find with this plugin is:

[:conditions, :include, :joins, :limit, :offset, :order, :select,
:readonly, :group, :from, :with_deleted]

Not sure how validates_uniqueness_of works. I’ll look into that next.