Check availability and uniqueness, rails 4

I have this code in my model:

  validates :user, presence: true, uniqueness: {scope: :project}

Individual validators, availability and uniqueness, each one works fine if there is no other. But if they are both present, as indicated above, the presence validator stops working. When I try to save an object with a null user, instead of getting a normal save error, I get an exception:

NoMethodError: undefined method 'attributes' for nil: NilClass from / usr / local / lib / ruby ​​/ gems / 2.0.0 / gems / activerecord-4.0.2 / lib / active_record / validations / uniqueness.rb: 56: in `build_relation ''

Is this a bug in ActiveRecord or am I doing something wrong?

+4
source share
1 answer

(https://github.com/thoughtbot/shoulda-matchers/issues/459), , / . allow_nil: true.

:

validates :user, presence: true, uniqueness: {scope: :project}, allow_nil: true

+4

All Articles