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.

accepts_nested_attributes_for raises NoMethodError when saving polymorphic belongs_to

#2318

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?"

Reported by Mike Breen · March 23rd, 2009 @ 10:22 PM

State: resolved
Milestone: 2.3.6
Assigned to: Eloy Duran Eloy Duran
Importance: none

Activity

  1. Eloy Duran
    Eloy Duran
    • Assigned user set to Eloy Duran

    March 23rd, 2009 @ 10:46 PM

  2. rollsteady
    rollsteady

    I think this might be the same problem that I am having as detailed in this post:

    http://railsforum.com/viewtopic....

    April 10th, 2009 @ 04:47 AM

  3. Eloy Duran
    Eloy Duran
    • State changed from new to verified
    • Milestone changed from 2.x to 2.3.4

    I have verified and applied this patch on my branch of 2-3-stable, which Micheal will look at and merge in before 2.3.4.

    http://github.com/alloy/rails/commit/ba0d70eb3ba313ded635a83ca9e3e1...

    Thanks!

    July 12th, 2009 @ 12:45 PM

  4. jamesw
    jamesw

    Will this fix work for the problems I'm having with has_manuy :through associations?
    https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets...

    August 11th, 2009 @ 01:40 PM

  5. Eloy Duran
    Eloy Duran

    @jamesw No it won't. This patch will simply raise an exception with a more human friendly messages. As the OP stated: ‘This patch will give a friendlier ArgumentError "Cannot build association looter. Are you trying to build a polymorphic one-to-one association?"’

    August 11th, 2009 @ 02:13 PM

  6. jamesw
    jamesw

    Thank you very much for clarifying that.

    August 11th, 2009 @ 02:40 PM

  7. Mike Breen
    Mike Breen

    @eloy Is there a reason we wouldn't put this in master also?

    August 13th, 2009 @ 12:35 AM

  8. jamesw
    jamesw

    @eloy, No I'm not trying to build a polymorphic one-to-one. Merely a nested association when the association is a has_many :through.I'm happy to provide further details of my exact scenario (rather than the contrived one I came up with for testing) just let me know.

    Sorry it took so long to respond to your question properly, I only just noticed it.

    August 13th, 2009 @ 02:22 AM

  9. Eloy Duran
    Eloy Duran

    @Mike Nope, no reason at all, good question :). My thought was that it would be pulled in by those working on master. I'll take that up with Michael though.

    @jamesw I didn't ask you a question, I quoted (see the quotes) the error message that will be raised when this patch is applied. Please read the whole ticket carefully to fullty understand what this ticket is about; it has nothing to do with your issues.

    August 13th, 2009 @ 08:49 AM

  10. rollsteady
    rollsteady

    @jamesw Check out http://railsforum.com/viewtopic.php?pid=93381#93381 This might be the same issue you are having. Haven't seen a fix that works.

    August 13th, 2009 @ 10:50 PM

  11. Mike
    Mike

    Not a fix, but a workaround:

    
    class Treasure < ActiveRecord::Base
      belongs_to :looter, :polymorphic => true
    
      accepts_nested_attributes_for :looter
    
      def build_looter(params)
        self.looter = xxx.new(params)
      end
    
    end
    

    To figure out the polymorphic class I passed a variable within the params hash (just a hidden field in the form).

    Works like a charm...

    August 21st, 2009 @ 02:11 PM

  12. metasoarous
    metasoarous

    Mike - your workaround didn't work for me. When I do something along those lines for my models and try to do the equivalent of

    @@@params = {:name => "pearl", :looter_attributes => {:catchphrase => "Arrr!"}} Treasure.new params@@@

    I no longer get the NoMethodError bit but instead an "ActiveRecord::UnknownAttributeError: unknown attribute: looter_attributes" in response. In fact I'm getting this same error for another nesting which operates through the has many side of a polymorphic relationship. Has anyone else experienced this issue?

    I saw Mike's general approach to solving the problem at http://www.travisdunn.com/rails-polymorphic-belongs_to-associations... and after getting to here found this post, so I thought I would share.

    Any ideas why I might be getting these errors and/or how to get rid of them?

    September 11th, 2009 @ 07:52 AM

  13. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.4 to 2.3.6

    [milestone:id#50064 bulk edit command]

    September 11th, 2009 @ 11:04 PM

  14. Repository
    Repository
    • State changed from verified to resolved

    (from [52a50db6c0183bf53c6760cce74529c13ed3e271]) Raise an exception with friendlier error message when attempting to build a polymorphic belongs_to with accepts_nested_attributes_for. [#2318 accepts_nested_attributes_for raises NoMethodError when saving polymorphic be... state:resolved] Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
    http://github.com/rails/rails/commit/52a50db6c0183bf53c6760cce74529...

    September 13th, 2009 @ 02:33 AM

  15. Repository
    Repository

    (from [bcd0ef710ec6d2cc6b880c39de0dfacc07df85e4]) Raise an exception with friendlier error message when attempting to build a polymorphic belongs_to with accepts_nested_attributes_for. [#2318 accepts_nested_attributes_for raises NoMethodError when saving polymorphic be... state:resolved] Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
    http://github.com/rails/rails/commit/bcd0ef710ec6d2cc6b880c39de0dfa...

    September 13th, 2009 @ 02:33 AM

  16. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.

    Automatic cleanup of spam.

    October 19th, 2010 @ 07:08 AM

  17. bingbing