Lephyrius
2013-Apr-03 09:56 UTC
validates uniqueness scope allow_blank/allow_nil -> validation error
I have this in a model:
class GenreBadge < ActiveRecord::Base
belongs_to :game, counter_cache: :genre_badges_count, touch: true
belongs_to :genre
validates :game_id, uniqueness: {scope: :genre_id}, allow_blank:
true
end
When I get one existing genre badge
genre_badge = game.genre_badges.first
genre_badge.game_id = nil
genre_badge.save!
It creates an validation exception:
"ActiveRecord::RecordInvalid: Validation failed: Game has already been
taken"
I already have have specified that I want to allow_blank. I really
don''t know why I get the validation error. I also tested allow_nil but
got the same error.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Андрей Большов
2013-Apr-04 13:54 UTC
Re: validates uniqueness scope allow_blank/allow_nil -> validation error
think it should be:
validates :game_id, uniqueness: {scope: :genre_id, allow_blank: true}
среда, 3 апреля 2013 г., 13:56:48 UTC+4 пользователь Lephyrius
написал:>
> I have this in a model:
> class GenreBadge < ActiveRecord::Base
>
> belongs_to :game, counter_cache: :genre_badges_count, touch: true
> belongs_to :genre
> validates :game_id, uniqueness: {scope: :genre_id}, allow_blank:
> true
>
> end
>
> When I get one existing genre badge
> genre_badge = game.genre_badges.first
> genre_badge.game_id = nil
> genre_badge.save!
>
> It creates an validation exception:
> "ActiveRecord::RecordInvalid: Validation failed: Game has already been
> taken"
>
> I already have have specified that I want to allow_blank. I really
> don''t know why I get the validation error. I also tested allow_nil
but
> got the same error.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/VGGosWbwnjQJ.
For more options, visit https://groups.google.com/groups/opt_out.