globalize / globalize-accessors

Easily access (read and write) globalize translated fields without fiddling with locale
Other
110 stars 43 forks source link

Undefined method `locale' for model using update_attributes #15

Closed phlegx closed 9 years ago

phlegx commented 9 years ago

Hi!

Rails: 4.1.7 Globalize: 4.0.2 Globalize Accessors: 0.1.5

The following code:

# /app/models/category.rb
...
translates :name
globalize_accessors
validates :name, presence: true, uniqueness: { scope: :locale }
# /db/seeds/../...rb

I18n.locale = :en
category = Category.find_or_initialize_by(name: name[:en])
category.update_attributes({
    name_de: name[:de],
    name_fr: name[:fr]
})

Produces this error:

NoMethodError: undefined method `locale' for #<Category:0x0000000885e398>

Works in console by doing:

2.1.4 :017 > c = Category.find_or_initialize_by(name: 'Test En')
2.1.4 :017 > c.name_de = 'Test De'
2.1.4 :017 > c.name_de
 => 'Test De'

Is the problem in the uniqueness validation of attribute name?

phlegx commented 9 years ago

Solved, because gem globalize 4.0.2 has a uniqueness validation for this purpose. But another question is open: Validation errors associated on translated attributes including locale https://github.com/globalize/globalize/issues/386

Any idea?

phlegx commented 9 years ago

SOLVED - Validation errors associated on translated attributes including locale https://github.com/globalize/globalize/issues/386

Added possible solution, here: https://github.com/globalize/globalize/issues/386#issuecomment-62715572