Reputation: 11799
In my Rails App, I have a Like model.
### like.rb
### Custom Validator Code:
class UniquenessValidator < ActiveModel::Validator
def validate(record)
# Empty
end
end
class Like < ActiveRecord::Base
include ActiveModel::Validations
validates_with UniquenessValidator
attr_accessible :user_id
belongs_to :likeable, polymorphic: true
belongs_to :user
end
In my rails console I try to do Like.all ( currently my Likes table is empty )
1.9.2p320 :001 > Like.all
RuntimeError: :attributes cannot be blank
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validator.rb:141:in `initialize'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activerecord-3.2.3/lib/active_record/validations/uniqueness.rb:7:in `initialize'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validations/with.rb:84:in `new'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validations/with.rb:84:in `block in validates_with'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validations/with.rb:83:in `each'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activemodel-3.2.3/lib/active_model/validations/with.rb:83:in `validates_with'
from /Users/user/Programming/WWW/Rails/experiments/test_app/app/models/like.rb:3:in `<class:Like>'
from /Users/user/Programming/WWW/Rails/experiments/test_app/app/models/like.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `load'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `block in load_file'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in `load_file'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:in `require_or_load'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in `load_missing_constant'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `each'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
1.9.2p320 :002 >
What is going on here ? (btw if I remove the validates_with UniquenessValidator
from Like.rb
, I don't get this error)
Upvotes: 1
Views: 1232
Reputation: 6198
I know it's not your case, but I got this error because I subclassed from EachValidator instead of Validator accidentally.
It was:
class UserProfileValidator < ActiveModel::EachValidator
def validate(record)
end
end
It should have been:
class UserProfileValidator < ActiveModel::Validator
def validate(record)
end
end
Or:
class UserProfileValidator < ActiveModel::EachValidator
def def validate_each(record, attribute, value)
end
end
Upvotes: 1
Reputation: 1888
Call your validator MyUniquenessValidator.
UniquenessValidator already exists in activerecord. This string from /Users/user/.rvm/gems/ruby-1.9.2-p320@test_app/gems/activerecord-3.2.3/lib/active_record/validations/uniqueness.rb:7:in
initialize'` is telling you about it.
Be careful with predefined ruby and RoR classes (data types (for example, "Complex"), rails validators, etc.)
Upvotes: 1