5

I am new to coding - and have not enough reputation to comment this answer: Rails 3: Uniqueness validation for nested fields_for

So I am creating this question as "Part 2" :)

I am a web designer but curious to learn coding, held with this from my days.

# app/validators/nested_attributes_uniqueness_validator.rb   
class NestedAttributesUniquenessValidator < ActiveModel::EachValidator
        record.errors[attribute] << "Products names must be unique" unless value.map(&:name).uniq.size == value.size
      end
end

above code with "ActiveModel::EachValidator" throw this error:

"undefined method `map' for "Area 1":String"


# app/validators/nested_attributes_uniqueness_validator.rb   
class NestedAttributesUniquenessValidator < ActiveModel::Validator
    record.errors[attribute] << "Products names must be unique" unless value.map(&:name).uniq.size == value.size
  end
end

above code with "ActiveModel::Validator" throw this error:

"Subclasses must implement a validate(record) method. "


this is model file:

class Area < ActiveRecord::Base


  validates :name,
            :presence => true,
            :uniqueness => {:scope => :city_id},
            :nested_attributes_uniqueness => {:field => :name}

  belongs_to :city

end

You can find complete code over here: https://github.com/syed-haroon/rose

peterh
  • 11,875
  • 18
  • 85
  • 108
Syed
  • 15,657
  • 13
  • 120
  • 154
  • Those validator classes look really weird - you're doing your check directly in the class rather than in appropriate instance method (as the other question you link to does) – Frederick Cheung May 20 '12 at 17:59
  • You've got different code in the repo for the validator than what you show here .. did you get it working? – Matenia Rossides May 21 '12 at 14:42
  • also i think you need to change this line https://github.com/syed-haroon/rose/blob/master/config/application.rb#L19 to include the `"#{Rails.root}/app/validators"` folder – Matenia Rossides May 21 '12 at 14:43
  • First of all, thanks to start supporting this issue: Yes its true that there is little changes in the files that i have committed and the code that i have showed here. Now there is no difference - pls check my repo. – Syed May 21 '12 at 18:07
  • @syed: I dont see any nested attributes in your model, though you are validating it (nested_attributes_uniqueness). As Alberto mentioned in the answer to your parent question # app/models/shop.rb class Shop < ActiveRecord::Base validates :products, :products_name_uniqueness => true end There should be 2 models which are associated like shops has many products, here in your code i dont see 2 models like that. hence it will be nice if you talk about what really you want to achieve so that i can tell you how to do it in rails way. – Krishna Prasad Varma May 22 '12 at 04:22

2 Answers2

1

@Syed: I think you are trying to do this. else reply to my comment.

# app/models/city.rb
class City < ActiveRecord::Base
  has_many :areas
  validates :areas, :area_name_uniqueness => true
end

# app/models/area.rb
class Area < ActiveRecord::Base
  validates_presence_of :name
  validates_uniqueness_of :name  
end

# config/initializers/area_name_uniqueness_validator.rb
class AreaNameUniquenessValidator < ActiveModel::Validator
  def validate_each(record, attribute, value)
    record.errors[attribute] << "Area names must be unique" unless value.map(&:name).uniq.size == value.size
  end
end
Krishna Prasad Varma
  • 4,670
  • 5
  • 29
  • 41
  • Hi @Krishnaprasad, thanks for helping out buddy and nice to meet you in stackoverflow :) I tried the way you mentioned about, but its throwing this error: "Subclasses must implement a validate(record) method." anyhow i have not committed that into my git repo. If you find time can you take a look into it. – Syed May 22 '12 at 18:51
1

I found the answer over here :

https://rails.lighthouseapp.com/projects/8994/tickets/2160-nested_attributes-validates_uniqueness_of-fails

&

validates_uniqueness_of in destroyed nested model rails

This is for rails 2, one line need to me modified over here: add_to_base has been deprecated and is unavailable in 3.1. Use self.errors.add(:base, message)

Community
  • 1
  • 1
Syed
  • 15,657
  • 13
  • 120
  • 154