PeterWong
PeterWong

Reputation: 16011

Validate uniqueness of globalized field

I have a model with translated fields using the globalize gem and globalize-accessors gem for providing localized attributes such as name_en, name_zh_hk for a localized name field.

for example:

class Person < ActiveRecord::Base
  translates :name
  globalize_accessors: locales: [:en, :"zh-HK"], attributes: [:name]

  # problem is:
  validates :name, presence: true, uniqueness: true
end

So now name_en and name_zh_hk can get and set the value in corresponding locale correctly.

However, the validates :name validates only the name field in Person model. I also want to validate the uniqueness of the chinese input.

In short, would like a (easy) way to validate uniqueness of both name_en and name_zh_hk

** I have a form to submit both name_en and name_hk.

Upvotes: 2

Views: 2322

Answers (4)

microweb10
microweb10

Reputation: 113

You have to do this

class Person < ActiveRecord::Base
  translates :name

  class Translation
    validates :name, presence: true, uniqueness: true
  end
end

Upvotes: 2

phlegx
phlegx

Reputation: 2732

Solved with the following code.

Model

# /app/models/category.rb

...
I18n.available_locales.each do |locale|
    validates :"name_#{locale}", presence: true, length: { maximum: 5 }, uniqueness: true
end

Validator

# config/initializers/associated_translations_uniqueness_validator.rb

require 'active_record'
require 'active_record/validations/uniqueness.rb'

ActiveRecord::Validations::UniquenessValidator.class_eval do
  def validate_each_with_associated_translations(record, attribute, value)
    klass = record.class
    if klass.translates? && !klass.translated?(attribute) && klass.globalize_attribute_names.include?(attribute)
      attribute_parts = attribute.to_s.rpartition('_')
      raw_attribute = attribute_parts.first.to_sym
      locale = attribute_parts.last.to_sym

      finder_class = klass.translation_class
      table = finder_class.arel_table

      relation = build_relation(finder_class, table, raw_attribute, value).and(table[:locale].eq(locale))
      relation = relation.and(table[klass.reflect_on_association(:translations).foreign_key].not_eq(record.send(:id))) if record.persisted?

      translated_scopes = Array(options[:scope]) & klass.translated_attribute_names
      untranslated_scopes = Array(options[:scope]) - translated_scopes

      untranslated_scopes.each do |scope_item|
        scope_value = record.send(scope_item)
        reflection = klass.reflect_on_association(scope_item)
        if reflection
          scope_value = record.send(reflection.foreign_key)
          scope_item = reflection.foreign_key
        end
        relation = relation.and(find_finder_class_for(record).arel_table[scope_item].eq(scope_value))
      end

      translated_scopes.each do |scope_item|
        scope_value = record.send(scope_item)
        relation = relation.and(table[scope_item].eq(scope_value))
      end

      if klass.unscoped.with_translations.where(relation).exists?
        record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
      end
    else
      validate_each_without_associated_translations(record, attribute, value)
    end
  end
  alias_method_chain :validate_each, :associated_translations
end

Upvotes: 0

drhenner
drhenner

Reputation: 2230

I could be confused what you are asking about unique scopes:

 validates :name, uniqueness: {scope: :blah}

specifically you may want to have a "PersonName" model.

PersonName

name | local | person_id

Person has_many :names

then have:

validates :name, uniqueness: { scope: :person_id }

this way if they enter a name for HK the same as the name for :en it will not be valid.

Upvotes: 0

bigpotato
bigpotato

Reputation: 27497

At the end of your person.rb model file (outside of class Person ... end, add this:

Person::Translation.class_eval do
  validates_presence_of :name
  validates_uniqueness_of :name
end

Upvotes: 2

Related Questions