This project is archived and is in readonly mode.

#2318 ✓resolved
Mike Breen

accepts_nested_attributes_for raises NoMethodError when saving polymorphic belongs_to

Reported by Mike Breen | March 23rd, 2009 @ 10:22 PM | in 2.3.6

If I have this relationship:

class Treasure < ActiveRecord::Base
  belongs_to :looter, :polymorphic => true

  accepts_nested_attributes_for :looter
end

class Pirate < ActiveRecord::Base
  has_one :treasures, :as => :looter
end

The following code will raise a NoMethodError "undefined method 'build_looter' for #<Treasure:0x23e8f94> :

Treasure.new(:name => 'pearl', :looter_attributes => {:catchphrase => "Arrr"})

This patch will give a friendlier ArgumentError "Cannot build association looter. Are you trying to build a polymorphic one-to-one association?"

Comments and changes to this ticket

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>

Referenced by

Pages