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

Single language field with accepts_nested_attributes_for not stored #31

Open
phlegx opened this issue Jan 3, 2017 · 2 comments
Open

Comments

@phlegx
Copy link

phlegx commented Jan 3, 2017

Hi!

I use the latest version of the gem. I have a view with three text fields, one for every language. If I want to submit the form where one text field (e.g. language field English) is filled out and the other two are empty, then the changes are not stored in database. I have checked the params and all three fields are present in the params. English field has a content the other two are empty.

Project has many tasks as nested fields. The model Project accepts nested attributes for tasks.

# controller
def update
    @project = Project.find(params[:id])
    @project.assign_attributes(params)
    ...
    @project.save
end

My Task model:

  ...
  translates :description
  globalize_accessors
  ...

If I delete the rows in table task_translations related to the tasks and re-submit the form, all works fine. Any idea?

@phlegx phlegx changed the title Single language field accepts_nested_attributes_for single language field Single language field with accepts_nested_attributes_for not stored Jan 3, 2017
@tommyku
Copy link

tommyku commented Jan 19, 2017

I encountered this when I set reject_if: name_is_blank as a parameter to accepts_nested_attributes_for

@yihyang
Copy link

yihyang commented May 8, 2019

Hi there, my reply seems to be 2 years late. But I would like to share my experience on what happened on my implementation that caused accepts_nested_attributes_for to not work.

Imagine the relation of a Product has many Promotion.

I have implemented a .each method on the promotions at the Product model's before_save. Which caused it to delegate to ActiveRecord's CollectionProxy method:

https://github.com/rails/rails/blob/v5.1.6.2/activerecord/lib/active_record/relation/delegation.rb#L39-L42

and therefore caused globalize to assign the attributes twice and somehow it assigned the old value to the dirty attribute on the second assignment.

There are 2 solutions for me:

  1. Execute @product.update twice (Hackish, but somehow it works)
  2. Remove the .each method on before_save

Hope this helps

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

No branches or pull requests

3 participants