This project is archived and is in readonly mode.

#5169 ✓invalid
petRUShka

Rails3: Nested attributes broke update_attributes

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

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

Comments and changes to this ticket

  • Subba

    Subba July 21st, 2010 @ 12:51 PM

    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"])
    
  • petRUShka

    petRUShka July 21st, 2010 @ 01:05 PM

    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>
    
    
    
    
  • Subba

    Subba July 21st, 2010 @ 01:09 PM

    can you post location model code

  • petRUShka

    petRUShka July 21st, 2010 @ 01:20 PM

    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?

  • Subba

    Subba July 21st, 2010 @ 06:58 PM

    • Assigned user set to “Santiago Pastorino”

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

  • Neeraj Singh

    Neeraj Singh July 26th, 2010 @ 08:36 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”
  • cuzic4n

    cuzic4n January 19th, 2011 @ 09:03 PM

    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.

  • cuzic4n

    cuzic4n January 19th, 2011 @ 09:43 PM

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

  • cuzic4n

    cuzic4n January 19th, 2011 @ 11:06 PM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages