I'm new to coding - and I don't have enough reputation to comment on this answer:
Rails 3: checking for uniqueness for nested_fields
So, I am creating this question as "Part 2" :)
Help me guys help me - I'm a web designer, but I'm curious to know how to do this with my days.
class NestedAttributesUniquenessValidator < ActiveModel::EachValidator
record.errors[attribute] << "Products names must be unique" unless value.map(&:name).uniq.size == value.size
end
end
the above code with "ActiveModel :: EveryValidator" will throw this error:
"undefined method` map 'for" Area 1 ": String"
class NestedAttributesUniquenessValidator < ActiveModel::Validator
record.errors[attribute] << "Products names must be unique" unless value.map(&:name).uniq.size == value.size
end
end
the above code with "ActiveModel :: Validator" will throw this error:
"Subclasses must implement a validation (write) method."
This is the model file:
class Area < ActiveRecord::Base
validates :name,
:presence => true,
:uniqueness => {:scope => :city_id},
:nested_attributes_uniqueness => {:field => :name}
belongs_to :city
end
Here you can find the full code:
https://github.com/syed-haroon/rose