Forum: Ruby on Rails validates_uniqueness_of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9054a70a5ebc5824bc2a0b5465e7b342?d=identicon&s=25 tmatsumoto (Guest)
on 2005-11-27 20:10
(Received via mailing list)
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
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 justin (Guest)
on 2005-11-27 22:11
(Received via mailing list)
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/...

Looks as if it is what you are looking for.

   Justin
Efa76b164a7de4a5730e4fa397cc4425?d=identicon&s=25 Michael Koziarski (Guest)
on 2008-09-19 05:29
(Received via mailing list)
> Has anyone any ideas ? :)
>
> 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
B935c7953e0d4c30b82fdee575163e6a?d=identicon&s=25 Pierre Valade (Guest)
on 2008-09-19 12:23
(Received via mailing list)
Thanks Michael, you were right !

Have a nice day, Pierre.

On Sep 18, 12:26 pm, "Michael Koziarski" <mich...@koziarski.com>
This topic is locked and can not be replied to.