Skip to content
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

Closed
JohnSmall opened this issue Jan 3, 2015 · 12 comments
Closed
Labels

Comments

@JohnSmall
Copy link

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.

@firedev
Copy link

firedev commented Jan 3, 2015

gem 'globalize', github: 'ncri/globalize'

@JohnSmall
Copy link
Author

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?

@JohnSmall
Copy link
Author

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?

@leighhalliday
Copy link

I'm also having some problems with Rails 4.2. I originally had the same ActiveSupport::HashWithIndifferentAccess error. I tried out ncri/globalize which fixed the one error, but now am running into another issue.

I still have a translated field title on the main posts table and also on my post_translations table. It doesn't seem to be setting the title field on the posts table when using ncri/globalize code... which is maybe telling me I shouldn't even have that field there anymore now that I am translating it. This seemed to work fine prior to Rails 4.2 though.

@OpakAlex
Copy link

OpakAlex commented Apr 3, 2015

@JohnSmall your solution very good!

@shioyama shioyama added the bug label Oct 1, 2016
@shioyama
Copy link
Contributor

shioyama commented Oct 1, 2016

I believe this is fixed now. @JohnSmall can I close this?

@layonferreira
Copy link

I'm still seeing this on my rails 4.2 project.
which branch got the fix @shioyama ?

@shioyama
Copy link
Contributor

shioyama commented Nov 9, 2016

@layonferreira Should be fixed on master.

@layonferreira
Copy link

@shioyama Thank you, master is indeed working for me

@tolhzar
Copy link

tolhzar commented Sep 26, 2019

Hey guys, I'm still getting this error with globalize 4.0.3 and rails 4.2. So no fix was provided outside master branch? Is there any way to use globalize with rails 4.2 then? Should we point to some specific commit? And if so to which one? @shioyama

@shioyama
Copy link
Contributor

I'm not working on this gem anymore, sorry.

@tolhzar
Copy link

tolhzar commented Sep 26, 2019

For anyone who has this issue in 2019 the following setup works:
gem 'globalize', git: 'https://github.com/globalize/globalize.git', ref: 'f1f1628'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants