This project is archived and is in readonly mode.
accepts_nested_attributes_for raises NoMethodError when saving polymorphic belongs_to
-
Eloy Duran
- Assigned user set to Eloy Duran
-
rollsteady
I think this might be the same problem that I am having as detailed in this post:
-
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!
-
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... -
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?"’
-
jamesw
Thank you very much for clarifying that.
-
Mike Breen
@eloy Is there a reason we wouldn't put this in master also?
-
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.
-
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.
-
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.
-
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 endTo 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
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?
-
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... -
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...
