On Fri, 20 Feb 2009 08:53:29 +0900
Makoto Kuwata <[email protected]> wrote:

> 
> Hi
> 
> Is it possible to specify multiple columns to :unique option?
> 
> For example:
> 
>  ## I want to validate uniqueness of :book_id AND :author_id,
>  ## but DataMapper validates them separately.
>  class Writing
>    include DataMapper::Resource
>    property :id, Serial
>    property :book_id,   Integer, :unique => :u1
>    property :author_id, Integer, :unique => :u1
>  end
> 
> *snip logs*
> --
> regards,
> makoto kuwata
> 

Yes, you can.

First, you can use ':unique_index => :u1' as in:

  property :book_id,   Integer, :unique_index => :u1
  property :author_id, Integer, :unique_index => :u1

This will define a unique index on the pair of columns in the database, which 
could be a useful protection.  To get the protection in ruby code too, you can 
use: 

  validates_is_unique :book_id, :scope => :author_id

which scopes the SELECT for book_id checking to the post's author_id.

Regards,
Jon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"DataMapper" 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/datamapper?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to