Hi guys, I''m having a strange error in one of my model relationships and it''s driving me crazy I have: class Target < ActiveRecord::Base has_many :sections end class Section < ActiveRecord::Base belongs_to :target has_many :line_items end class LineItem < ActiveRecord::Base belongs_to :section end When I do the following in script/console: s = LineItem.last.section (s.id is 21, as is LineItem.last.section_id as well) s.target I get the Section instance, and the logger shows no query to the targets table. However, if I do: s = Section.find(21) s.target I get the right Target instance. Even more strange is that if I do: s = LineItem.last.section s.target.target (twice the target message) I get the right Target instance. I''m using Rails 2.3.5 and I''m getting this warning, but I''m not sure if it has something to do with it: vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:217: warning: default `to_a'' will be obsolete Any clue? Thanx a lot in advance. -- Leonardo Mateo. There''s no place like ~ -- 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.
On Jul 19, 9:26 pm, Leonardo Mateo <leonardoma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> When I do the following in script/console: > s = LineItem.last.section (s.id is 21, as is LineItem.last.section_id as well) > s.target > > I get the Section instance, and the logger shows no query to the > targets table. However, if I do: > s = Section.find(21) > s.target > > I get the right Target instance. > > Even more strange is that if I do: > s = LineItem.last.section > s.target.target (twice the target message) > > I get the right Target instance. >If you do some_line_item.section what you get back isn''t a section but an association proxy. If you do anything that actually requires the section object, active record will load it and send the method calls to it. Unfortunately for you the method on association proxy that returns the actual object for the association is called target. Fred> I''m using Rails 2.3.5 and I''m getting this warning, but I''m not sure > if it has something to do with it: > vendor/rails/activerecord/lib/active_record/associations/association_proxy. rb:217: > warning: default `to_a'' will be obsolete > > Any clue? > > Thanx a lot in advance. > > -- > Leonardo Mateo. > There''s no place like ~-- 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.
On 19 July 2010 22:09, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Jul 19, 9:26 pm, Leonardo Mateo <leonardoma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> When I do the following in script/console: >> s = LineItem.last.section (s.id is 21, as is LineItem.last.section_id as well) >> s.target >> >> I get the Section instance, and the logger shows no query to the >> targets table. However, if I do: >> s = Section.find(21) >> s.target >> >> I get the right Target instance. >> >> Even more strange is that if I do: >> s = LineItem.last.section >> s.target.target (twice the target message) >> >> I get the right Target instance. >> > > If you do some_line_item.section what you get back isn''t a section but > an association proxy. If you do anything that actually requires the > section object, active record will load it and send the method calls > to it. Unfortunately for you the method on association proxy that > returns the actual object for the association is called target.See http://wiki.rubyonrails.org/rails/pages/ReservedWords for more words you can''t use. Colin -- 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.
On Tue, Jul 20, 2010 at 4:46 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 19 July 2010 22:09, Frederick Cheung <frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >> On Jul 19, 9:26 pm, Leonardo Mateo <leonardoma...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> When I do the following in script/console: >>> s = LineItem.last.section (s.id is 21, as is LineItem.last.section_id as well) >>> s.target >>> >>> I get the Section instance, and the logger shows no query to the >>> targets table. However, if I do: >>> s = Section.find(21) >>> s.target >>> >>> I get the right Target instance. >>> >>> Even more strange is that if I do: >>> s = LineItem.last.section >>> s.target.target (twice the target message) >>> >>> I get the right Target instance. >>> >> >> If you do some_line_item.section what you get back isn''t a section but >> an association proxy. If you do anything that actually requires the >> section object, active record will load it and send the method calls >> to it. Unfortunately for you the method on association proxy that >> returns the actual object for the association is called target.Thanks a lot, Frederick. Just the words I needed.> > See http://wiki.rubyonrails.org/rails/pages/ReservedWords for more > words you can''t use.Thanks Colin!, great tip! I didn''t know this. -- Leonardo Mateo. There''s no place like ~ -- 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.