Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails3: Nested attributes broke update_attributes

#5169

I have two models, cities and city_prices:

class City < ActiveRecord::Base
  has_many :city_prices, :dependent => :destroy
  accepts_nested_attributes_for :city_prices
end
class CityPrice < ActiveRecord::Base
  belongs_to :city
end

I have a standard controller with update action:

    def update
      @city = City.find(params[:id])
      respond_to do |format|
        if @city.update_attributes(params[:city]) 
          format.html { redirect_to(admin_cities_path)) }
        else
          format.html { render :action => "edit" }
        end
      end
    end

But when I update city with params:

{"city"=>{"city_prices_attributes"=>{"6"=>{"price"=>"",
 "id"=>"7"},
 "11"=>{"price"=>"",
 "id"=>"12"},
 "7"=>{"price"=>"",
 "id"=>"8"},
 "12"=>{"price"=>"",
 "id"=>"13"},
 "5"=>{"price"=>"1000",
 "id"=>"6",
}}},
 "commit"=>"save",
 "authenticity_token"=>"Pzspi92dKRn63NvEP02mKxw0JJ4AT9WOHpK2LBJ9SJI=",
 "_method"=>"put",
 "id"=>"5"}

I get en error.

  
NoMethodError (undefined method `to_sym' for true:TrueClass):  
app/controllers/admin/cities_controller.rb:11:in `update'  
app/controllers/admin/cities_controller.rb:8:in `update'


Full trace:  
https://gist.github.com/c2af2c8066dbbe1b48cd

Rails3 beta4  
Ruby 1.8.7 and same issue on Ruby 1.9.3 form svn

Reported by petRUShka · July 21st, 2010 @ 12:16 PM

State: invalid
Milestone: none
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Subba
    Subba

    i tested with below code. it worked for me.
    could post all the model code with callbacks if any.

        city = City.create(:name => "New York")
        soap = city.city_prices.create(:price => 10.0)
        beer = city.city_prices.create(:price => 20.0)
        attributes = {
          "city"=>{
            "city_prices_attributes"=>{
              "6"=> {"price"=>"", "id"=> soap.id.to_s},
              "4"=> {"price"=>"", "id"=> beer.id.to_s},
            }
          }
        }
        city.update_attributes(attributes["city"])
    

    July 21st, 2010 @ 12:51 PM

  2. petRUShka
    petRUShka

    Full code of model CityPrice with callbacks:

    class CityPrice < ActiveRecord::Base
      belongs_to :city
    
      after_update :destroy_if_price_blank
    
      private
    
      def destroy_if_price_blank  
        self.destroy if self.price.blank?
      end
    end
    

    Full code of city:

    class City < ActiveRecord::Base
      has_many :locations, :dependent => :destroy
      has_many :city_prices, :dependent => :destroy
    
      accepts_nested_attributes_for :locations
      accepts_nested_attributes_for :city_prices
    end
    

    I tried your example in rails console, same error, as expected:

     from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1296:in expand_hash_conditions_for_aggregates'
    
    
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1295:in `each'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1295:in `expand_hash_conditions_for_aggregates'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/query_methods.rb:215:in `send'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/query_methods.rb:215:in `build_where'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/query_methods.rb:42:in `where'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/spawn_methods.rb:92:in `apply_finder_options'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/relation/finder_methods.rb:138:in `all'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:403:in `__send__'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:403:in `all'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_collection.rb:421:in `send'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_collection.rb:421:in `method_missing'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1167:in `with_scope'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_proxy.rb:201:in `send'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_proxy.rb:201:in `with_scope'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/associations/association_collection.rb:417:in `method_missing'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/nested_attributes.rb:358:in `assign_nested_attributes_for_collection_association'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/nested_attributes.rb:253:in `city_prices_attributes='
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1586:in `send'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1586:in `attributes='
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1582:in `each'
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/base.rb:1582:in `attributes='
    from /home/petrushka/.bundle/ruby/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/persistence.rb:110:in `update_attributes'</code>
    
    
    
    

    July 21st, 2010 @ 01:05 PM

  3. Subba
    Subba

    can you post location model code

    July 21st, 2010 @ 01:09 PM

  4. petRUShka
    petRUShka

    It's seemed that I found a source of error. I remove to-csv plugin from app and error was disappeared.
    I create an issue in issue tracker of this plugin: http://github.com/ilmotta/to-csv/issues/#issue/5

    But why there are no lines of this plugin in backtrace?

    July 21st, 2010 @ 01:20 PM

  5. Subba
    Subba
    • Assigned user set to Santiago Pastorino

    we can mark this invalid as the problem is related to-csv plugin.

    July 21st, 2010 @ 06:58 PM

  6. Neeraj Singh
    Neeraj Singh
    • State changed from new to invalid
    • Importance changed from to Low

    July 26th, 2010 @ 08:36 PM

  7. cuzic4n
    cuzic4n

    This same thing happens to be on ruby 1.8.6 & rails 2.3.8 on winxp. I updated the to-csv issue on github as well.

    January 19th, 2011 @ 09:03 PM

  8. cuzic4n
    cuzic4n

    I meant ruby 1.8.7
    ruby 1.8.7 (2010-01-10 patchlevel 249) [i386-mingw32]

    January 19th, 2011 @ 09:43 PM

  9. cuzic4n
    cuzic4n

    tried with latest 1.8.7 patchlevel.. still same prob
    ruby 1.8.7 (2010-12-23 patchlevel 330) [i386-mingw32]

    January 19th, 2011 @ 11:06 PM