validates_uniqueness_of fails with non-ID arguments #4321

Closed
cptobvious opened this Issue Jan 5, 2012 · 9 comments

Comments

Projects
None yet
9 participants

Example:

class Foo < ActiveRecord::Base
  belongs_to :a
  belongs_to :b

  validates_presence_of :a
  validates_presence_of :b

  validates_uniqueness_of :a, :scope => :b # fails with "NoMethodError: undefined method `text?' for nil:NilClass"
  # activesupport-3.0.11/lib/active_support/whiny_nil.rb:48:in `method_missing'

  validates_uniqueness_of :a_id, :scope => :b_id # works
end

tenderlove was assigned Jan 5, 2012

Contributor

dnagir commented Jan 5, 2012

+1 for this. Being asking about it at SO.

ekosz commented Jan 6, 2012

+1 Just ran into this today. Very Un-Rails like.

Contributor

dpickett commented Jan 7, 2012

there are two issues at play here, one for setting a scope to an association, and one for setting the actual unique attribute. I've introduced two commits attacking each issue individually.

Owner

tenderlove commented Jan 9, 2012

I've merged @dpickett's pull request, so closing this.

tenderlove closed this Jan 9, 2012

@rafaelfranca rafaelfranca pushed a commit to rafaelfranca/omg-rails that referenced this issue Jan 10, 2012

Dan Pickett allow an association as a scope parameter 5f47278

@rafaelfranca rafaelfranca pushed a commit to rafaelfranca/omg-rails that referenced this issue Jan 10, 2012

Dan Pickett allow association as 1st uniqueness validation arg 3b5fbaf
Contributor

joneslee85 commented Apr 27, 2013

I still experience this issue with Rails 3.2.13, I think there is regression.

@joneslee85, the bug has not been fixed in the 3.2 branch.

As a quick&dirty solution try https://gist.github.com/mperrando/5503126

@tenderlove, any chances to see this PR cherry-picked on the 3.2 branch?

The overall diffused solution to this problem (i.e. validating against :<assoc_name>_id, see this search) is annoying; first of all it is very un-rails (as @ekosz says) and then because the errors are not recorded on the :<assoc_name>, and, as a consequence, they are not represented in the view!

Contributor

joneslee85 commented May 3, 2013

@tenderlove: yes, please get this cherry-pick for 3.2.14

Sent from Mailbox for iPhone

On Fri, May 3, 2013 at 1:45 AM, marco notifications@github.com wrote:

@joneslee85, the bug has not been fixed in the 3.2 branch.
As a quick&dirty solution try https://gist.github.com/mperrando/5503126
@tenderlove, any chances to see this PR cherry-picked on the 3.2 branch?

The overall diffused solution to this problem (i.e. validating against :<assoc_name>_id, see this search) is annoying; first of all it is very un-rails (as @ekosz says) and then because the errors are not recorded on the <assoc_name>, and, as a consequence, they are not represented in the view!

Reply to this email directly or view it on GitHub:
#4321 (comment)

s2t2 commented Jun 5, 2017

I think I just ran into this error.

Owner

eileencodes commented Jun 5, 2017

@s2t2 this issue is old and closed. If you're experiencing this on Rails 5 please open a new issue with an executable test script that reproduces the error you're seeing. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment