0

I'm fighting with this bug for the past few hours and I can't make sense of it and my researches didn't give an answer.

It is a basic HABTM relationship. Inputs HABTM Visualizations, and I have a cross table InputsVisualizations that has some attributes of its own.

    = form_for(@visualization) do |f|

      = f.input :title

      = f.fields_for :inputs_visualizations do |iv|

        = iv.input :color

        = iv.fields_for :input do |i|
          = i.input :title

      = f.button :submit, "Save"


    class Input < ActiveRecord::Base

      # Associations ------------------
      has_many :inputs_visualizations, dependent: :destroy, order: "inputs_visualizations.order ASC"
      has_many :visualizations, through: :inputs_visualizations

      # Attributes --------------------
      attr_accessible :title, :unit

    end


    class InputsVisualization < ActiveRecord::Base

      # Associations ------------------
      belongs_to :input
      belongs_to :visualization

      # Attributes --------------------
      attr_accessible :input_id, :visualization_id, :color, :input_attributes
      accepts_nested_attributes_for :input, :reject_if => lambda { |i| i[:title].blank? }, :allow_destroy => true

    end

    class Visualization < ActiveRecord::Base

      # Associations ------------------
      has_many :inputs_visualizations, dependent: :destroy, order: "inputs_visualizations.order ASC"
      has_many :inputs, through: :inputs_visualizations, order: "inputs_visualizations.order ASC"

      # Attributes --------------------
      attr_accessible :title, :inputs_visualizations_attributes
      accepts_nested_attributes_for :inputs_visualizations, :reject_if => lambda { |a| a[:input_id].blank? }, :allow_destroy => true

    end

I need a form for Visualizations that let me manage both InputsVisualizations and Inputs. As you can see in my form, there are two nested fields_for.

Case 1: I create a nested InputsVisualization with a nested Input (both are new_record). I save the form, they both are created. Cool!

Case 2: From the same form, I update an Input (existing record). I save, nothing is updated even though the attributes are properly passed to the controller.

I read that nested_attributes don't work with belongs_to relationship, though it created it just fine. Why doesn't it update afterwards?

Thanks

karellm
  • 1,843
  • 1
  • 20
  • 23
  • Please include code details in the question so that future Stack Overflow readers can understand the problem even if your github entry goes away. I see that you have a `:reject_if` condition in `visualization.rb` that looks for `:input_id` but this value isn't a part of your form. What is this `:reject_if` checking? Also, can you share the code from your controller? – cschroed Nov 21 '13 at 02:26
  • Thanks, that was indeed the cause of my problem. – karellm Nov 21 '13 at 23:08

1 Answers1

1

The :reject_if condition on this line looks for an :input_id but that value is not included in the form. So this could prevent the update from going through.

accepts_nested_attributes_for :inputs_visualizations, :reject_if => lambda { |a| a[:input_id].blank? }, :allow_destroy => true
cschroed
  • 6,304
  • 6
  • 42
  • 56