This project is archived and is in readonly mode.
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
-
Eloy Duran March 23rd, 2009 @ 10:46 PM
- Assigned user set to Eloy Duran
-
rollsteady April 10th, 2009 @ 04:47 AM
I think this might be the same problem that I am having as detailed in this post:
-
Eloy Duran July 12th, 2009 @ 12:45 PM
- 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!
-
jamesw August 11th, 2009 @ 01:40 PM
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... -
Eloy Duran August 11th, 2009 @ 02:13 PM
@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?"’
-
Mike Breen August 13th, 2009 @ 12:35 AM
@eloy Is there a reason we wouldn't put this in master also?
-
jamesw August 13th, 2009 @ 02:22 AM
@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.
-
Eloy Duran August 13th, 2009 @ 08:49 AM
@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.
-
rollsteady August 13th, 2009 @ 10:50 PM
@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.
-
Mike August 21st, 2009 @ 02:11 PM
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...
-
metasoarous September 11th, 2009 @ 07:52 AM
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?
-
Jeremy Kemper September 11th, 2009 @ 11:04 PM
- Milestone changed from 2.3.4 to 2.3.6
[milestone:id#50064 bulk edit command]
-
Repository September 13th, 2009 @ 02:33 AM
- 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 state:resolved] Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
http://github.com/rails/rails/commit/52a50db6c0183bf53c6760cce74529... -
Repository September 13th, 2009 @ 02:33 AM
(from [bcd0ef710ec6d2cc6b880c39de0dfacc07df85e4]) Raise an exception with friendlier error message when attempting to build a polymorphic belongs_to with accepts_nested_attributes_for. [#2318 state:resolved] Signed-off-by: Eloy Duran eloy.de.enige@gmail.com
http://github.com/rails/rails/commit/bcd0ef710ec6d2cc6b880c39de0dfa...
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>
People watching this ticket
Attachments
Referenced by
- 2318 accepts_nested_attributes_for raises NoMethodError when saving polymorphic belongs_to (from [52a50db6c0183bf53c6760cce74529c13ed3e271]) Raise a...
- 2318 accepts_nested_attributes_for raises NoMethodError when saving polymorphic belongs_to (from [bcd0ef710ec6d2cc6b880c39de0dfacc07df85e4]) Raise a...