New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails 4.2 Can't modify frozen hash ActiveSupport::HashWithIndifferentAccess #393
Comments
|
Ah, thanks for such a quick response. I guessed someone must be on it. I also tried a quick job myself simply using ActiveRecord magic. I tried delegation to a record selected by the current locale. That works for reading but writes don't get saved. After messing with scopes I finally got this to work. class Brand < ActiveRecord::Base
has_many :translations,autosave: true
def translation
translations.find_or_create_by(locale: I18n.locale)
end
def text_3
translation.text_3
end
def text_3=(attr)
t = translation
t.text_3=attr
t.save
end
end It's not pretty and I'm sure it could be compressed into one line by someone who has deep knowledge of AR associations, but it does work. I've not looked at the internals of Globalize, but if this is so simple what so of thing is it doing that fails in Rails 4.2? |
A few more minutes on this and I came up with class Brand < ActiveRecord::Base
has_many :translations,autosave: true
def translation
translations.find_or_create_by(locale: I18n.locale)
end
%w(text_3 text_4 text_5).each do |fld_name|
define_method(fld_name){translation.send(fld_name)}
define_method("#{fld_name}=") do |text|
t = translation
t.send("#{fld_name}=",text)
t.save
end
end
end That gives me three translated fields on the Brand model. Which for my purposes is good enough, it does the job and it means I have to look after the translation table myself, which I'll have to do anyway as I'm going to be adding Postgresql tsvector fields for full text search. Having the translation table hidden away in a gem might get tricky if there are extra fields I want to maintain. Is there any special stuff the Globalize gem does that increases performance over the standard ActiveRecord methods? |
I'm also having some problems with Rails 4.2. I originally had the same I still have a translated field |
@JohnSmall your solution very good! |
I believe this is fixed now. @JohnSmall can I close this? |
I'm still seeing this on my rails 4.2 project. |
@layonferreira Should be fixed on |
@shioyama Thank you, |
Hey guys, I'm still getting this error with globalize 4.0.3 and rails 4.2. So no fix was provided outside |
I'm not working on this gem anymore, sorry. |
For anyone who has this issue in 2019 the following setup works: |
Whoa!! A killer. Globalize 4.0.3 will not work at all with Rails 4.2. I just tried to create my first record in 4.2 with Globalize 4.0.3 and got the error
Can't modify frozen hash ActiveSupport::HashWithIndifferentAccess
I noticed people having problems with Rails 4.2.rcxx but they seem to be solved. This one isn't. Acts_as_taggable have the same problem, see mbleigh/acts-as-taggable-on#580, that was back in August.
The text was updated successfully, but these errors were encountered: