nfriend21
nfriend21

Reputation: 2200

Rails Nested Attribute Not Updating Model, But Params are Correct

I have a nested form that will not update for whatever reason, and no update is being thrown, and the form params appear correct.

The problem is with "medium_prices_attributes"

Here's the params:"store"=> {"style_price_list_attributes"=> {"frame_low_cost"=>"4.00", "frame_middle_cost"=>"5.00", "frame_high_cost"=>"6.00", "id"=>"53", "color_styles_avail"=>"true", "frame_avail"=>"true", "stretcher_bars_75"=>"5.00", "stretcher_bars_75_avail"=>"true", "stretcher_bars_1_25"=>"5.00", "stretcher_bars_1_25_avail"=>"false", "stretcher_bars_1_5"=>"5.00", "stretcher_bars_1_5_avail"=>"true", "stretcher_bars_1_75"=>"5.00", "stretcher_bars_1_75_avail"=>"false" }, "shipping_price_list_attributes"=> {"shipping_prices"=>{"minimum_price"=>"5.00", "price_for_print_only"=>"7.00", "price_wrapped"=>"", "price_framed"=>""}, "id"=>"52"}, "medium_prices_attributes"=> {"0"=>{"price_per_sq_ft"=>"10.00", "id"=>"2458"}, "1"=>{"price_per_sq_ft"=>"10.00", "id"=>"2466"}, "2"=>{"price_per_sq_ft"=>"10.00", "id"=>"2467"}, "3"=>{"price_per_sq_ft"=>"99", "id"=>"2457"}, "4"=>{"price_per_sq_ft"=>"99", "id"=>"2459"}, "5"=>{"price_per_sq_ft"=>"111.00", "id"=>"2465"}}, "medium_availabilities_attributes"=> {"0"=>{"id"=>"4005"}, "1"=>{"id"=>"4006"}, "2"=>{"id"=>"4010"}, "3"=>{"id"=>"4009"}, "4"=>{"id"=>"4011"}, "5"=>{"id"=>"4012"}}, "page_attributes"=> {"name"=>"Store22", "headline_text"=>"Store", "id"=>"1124"}, "paypal_email"=>"", "currency"=>"USD", "default_sizes"=>"6x8"}, "page"=>{"online_status"=>"true", "pswd"=>"", "active_billboard_id"=>"", "title"=>"Store", "meta_keywords"=>"", "meta_description"=>"", "slug"=>"store", "script_head"=>"", "script_body"=>""}, "id"=>"123"}

My store.rb has:

attr_accessible :medium_prices_attributes

has_many :medium_prices

accepts_nested_attributes_for :medium_prices

As you can see in my params, the style_price_list_attributes and shipping_price_list_attributes are there, and they update correctly. It's only the medium_prices that have the problem.

When I try to update the attributes in the console, I get a "true" response, however the value of that medium_price record doesn't change. For example:

Store.find(163).update_attributes("medium_prices_attributes"=>{"1"=>{"price_per_sq_ft"=>"122.00", "id"=>"2457"}})

(0.2ms)  BEGIN
(0.5ms)  UPDATE "stores" SET "updated_at" = '2013-12-12 15:19:24.042735',         "default_sizes" = '---
- 6x8
' WHERE "stores"."id" = 123
   (0.3ms)  COMMIT
=> true

Thanks!

Upvotes: 1

Views: 49

Answers (1)

nfriend21
nfriend21

Reputation: 2200

After much turmoil, it turns out I had

has_many :medium_prices

listed twice on my Store model. The list was pretty long so I didn't catch it at the time. Rails doesn't throw an error, so this was hard to track down.

Upvotes: 1

Related Questions