BarryOg
2010-May-27 11:18 UTC
Can only traverse a polymorphic association in one direction
Hi all, I''m working on a polymorphic association and I''ve run into a bit of a stumbling block. I only seem to be able to traverse the relationship in one direction and not the other. This is what my code looks like: class NodeEntry < ActiveRecord::Base acts_as_nested_set belongs_to :node, :polymorphic => true end class Location < ActiveRecord::Base attr_accessor :name has_one :node_entry, :as => :node validates_presence_of :country, :region, :name end class Meter < ActiveRecord::Base attr_accessor :name, :parent_id has_one :node_entry, :as => :node validates_presence_of :meter_identifier, :type_of_meter end class MeterGroup < ActiveRecord::Base has_one :node_entry, :as => :node end When I create a new location or meter I use the following code: @location = Location.new(params[:location]) @location_node = NodeEntry.new(:name => params[:location][:name]) @location_node.node = @location @location_node.save After loading a location or meter object from the database, when I attempt to do a location.node I get an error saying the object doesn''t have that method. When I load a node entry object I have no problem accessing its node and I get the correct class back. I can get around this by doing a simple query such as: NodeEntry.first(:conditions => "node_id = #{loc.id} and node_type ''Location''").name but I had assumed active record should be doing this for me under the covers. Have I made a mistake somewhere or is this the expected behavior of polymorphic associations? -- View this message in context: http://old.nabble.com/Can-only-traverse-a-polymorphic-association-in-one-direction-tp28692572p28692572.html Sent from the RubyOnRails Users mailing list archive at Nabble.com. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matt Jones
2010-May-29 21:46 UTC
Re: Can only traverse a polymorphic association in one direction
On May 27, 7:18 am, BarryOg <barryodrisc...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, I''m working on a polymorphic association and I''ve run into a bit of a > stumbling block. I only seem to be able to traverse the relationship in one > direction and not the other. This is what my code looks like: > > class NodeEntry < ActiveRecord::Base > acts_as_nested_set > belongs_to :node, :polymorphic => true > end > > class Location < ActiveRecord::Base > attr_accessor :name > has_one :node_entry, :as => :node > validates_presence_of :country, :region, :name > end > > class Meter < ActiveRecord::Base > attr_accessor :name, :parent_id > has_one :node_entry, :as => :node > validates_presence_of :meter_identifier, :type_of_meter > end > > class MeterGroup < ActiveRecord::Base > has_one :node_entry, :as => :node > end > > When I create a new location or meter I use the following code: > @location = Location.new(params[:location]) > @location_node = NodeEntry.new(:name => params[:location][:name]) > @location_node.node = @location > @location_node.save > > After loading a location or meter object from the database, when I attempt > to do a location.node I get an error saying the object doesn''t have that > method.The has_one is still named what you''ve specified (:node_entry in this case), the polymorphic stuff doesn''t change that... So it''d be: @location.node_entry not @location.node --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
BarryOg
2010-Jun-03 09:51 UTC
Re: Can only traverse a polymorphic association in one direction
Thanks for taking the time to read through my code and spot the obvious, I guess too much coffee and too little sleep is not the best recipe. Matt Jones wrote:> > > The has_one is still named what you''ve specified (:node_entry in this > case), the polymorphic stuff doesn''t change that... > > So it''d be: > > @location.node_entry > > not > > @location.node > > > --Matt Jones > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- View this message in context: http://old.nabble.com/Can-only-traverse-a-polymorphic-association-in-one-direction-tp28692572p28765373.html Sent from the RubyOnRails Users mailing list archive at Nabble.com. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.