grosser/translated_attributes — Issue #2: Translation model through association not always loaded
Author: philly-mac
Created: 2012-02-10 10:34:07+00:00
State: open

Hi,

There is an issue that randomly happens in the method

````ruby
def store_translated_attributes
return true unless @translated_attributes_changed
translations.delete_all
@translated_attributes.each do |locale, attributes|
attributes.each do |attribute, value|
next if value.blank?
next unless self.class.translated_attributes_options[:fields].include? attribute.to_sym
translations.create!(translated_attributes_options[:attribute_column] => attribute, :text=>value, :language=>locale)
end
end
@translated_attributes_changed = false
true
end
````

the line
````ruby
translations.create!(translated_attributes_options[:attribute_column] => attribute, :text=>value, :language=>locale)
````

throws the error

````unknown attribute: attribute````

This seems to be because the translation model through the association is not loaded yet.
Doing a

````ruby
translations.new
````

fixes the problem.

So could I suggest a fix like this

````ruby
translation = translations.new
translation.attributes = {translated_attributes_options[:attribute_column] => attribute, :text => value, :language => locale}
translation.save!
````

This solves the problem by doing the load of the model through the association and stops the error from happening.

If you have a better idea of how to fix this could you let me know, or even better implement it so I could do a pull of the new code?

Thanks