Validates_uniqueness_of

Hi,

I’m trying to figure some stuff out about using
validates_uniqueness_of, particularly if it is possible to validate a
unique object in a database and rather than throwing an error like
“name already exist”, use the primary key of the already existing
object to pass along to join tables.

So far many of the examples I’ve come across has
validates_uniqueness_of stopping if the object is not unique.

What happens if there is a table of authors joined to a table of
articles, shouldn’t each author in the authors table be unique but
still joined by its id if there is a new article by that author?

Thanks,

Todd

tmatsumoto wrote:

What happens if there is a table of authors joined to a table of
articles, shouldn’t each author in the authors table be unique but still
joined by its id if there is a new article by that author?

There’s a new find_or_create_by_X feature in the latest release
candidate of Rails… I couldn’t find it in the ActiveRecord
documentation, but DHH mentioned it in the Rails blog here:

http://weblog.rubyonrails.com/articles/2005/11/07/rails-1-0-rc4-0-14-3-its-the-final-countdown

Looks as if it is what you are looking for.

Justin

Thanks Michael, you were right !

Have a nice day, Pierre.

On Sep 18, 12:26 pm, “Michael K.” [email protected]

Has anyone any ideas ? :slight_smile:

Thank you very much,

Have you overridden the method exists? on that model? The default
implementation takes one argument and your stacktrace implies that
it’s expecting two. The uniqueness validation relies on this method
now, which is why you didn’t see it earlier.


Cheers

Koz