On 28 Aug 2010, at 20:32, Anthony Smith wrote:

> Has anybody noticed validates_uniqueness_of missing in rails 3.0 rc2? I'm not 
> sure if this is a bug or I'm just doing something wrong.

It's still in there, as far as I can see. If it had been removed, you'd get an 
error when you try to call 'validates_uniqueness_of'.

> Here's what I'm doing.
> 
> Class 1:
> 
> class Title < ActiveRecord::Base
>   validates_length_of     :language, :is => 3
>   validates_uniqueness_of :language, :scope => :book_id
>   validate                :language, :is_proper_language_code
>   
>   validates_presence_of   :value
>   
>   belongs_to              :book
>   
>   def is_proper_language_code
>     errors.add(:base, "invalid language code") unless 
> !ISO_639::ISO_639_2.find{ |lang| lang[0] == self.language }.nil?
>   end
> end
> 
> Class 2:
> 
> class Book < ActiveRecord::Base
>   attr_accessible       :titles
>   
>   validates_presence_of :titles
>   
>   has_many              :titles
> end
> 
> Spec:
> 
> before(:each) do
>     @valid_title_attributes = {:book_id => 1, :language => "eng", :value => 
> "..."}
>     
>     @book = Book.new
>     @book.titles.push(Title.new(@valid_title_attributes))
> end
> 
> it "should not have multiple titles with same language attribute" do
>       @book.titles.clear
>       @book.titles.push(Title.new(@valid_title_attributes))
>       @book.titles.push(Title.new(@valid_title_attributes))
>       @book.should_not be_valid
>       
>       @book.titles.clear
>       @book.titles.push(Title.new(@valid_title_attributes))
>       @book.should be_valid
> end

I assume the problem is that "@book.should_not be_valid" is failing? The 
uniqueness validator works by querying the database for other matching records. 
So if you're just creating titles in memory, and not saving them, the 
uniqueness validator won't complain about them. Try changing your 'Title.new's 
to 'Title.create'.

Chris


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" 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-talk?hl=en.

Reply via email to