Here''s an example: class LineItem < ActiveRecord::Base belongs_to :order end class Order < ActiveRecord::Base has_many :line_items end my_line_item = LineItem.find(:zipcode => "12345") Order.find_by_line_item(my_line_item) # doesn''t work Order.find_by_line_item_id (my_line_item) # does work I was surprised by this, because it seems to force me to make reference to the id field of the line item, rather than referring to the LineItem as a class or type in the Order.findxxx statement. Am I misunderstanding something or missing a trick? Thanks!! -- Pito -- Posted via http://www.ruby-forum.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.
Pito Salas wrote:> Here''s an example: > > class LineItem < ActiveRecord::Base > belongs_to :order > end > > class Order < ActiveRecord::Base > has_many :line_items > end > > my_line_item = LineItem.find(:zipcode => "12345") > > Order.find_by_line_item(my_line_item) # doesn''t work > > Order.find_by_line_item_id (my_line_item) # does work >Probably not explaining this clearly enough (I blame the distillery masters at Lagavulin), but: find_by_line_item fails - my_line_item is an instance of the class (an object) that does not have a ''line_item'' attribute you indicated, it *is* a LineItem find_by_line_item_id works - rails infers the id attribute when you pass an object (my_line_item), and in this case you''ve provided enough information to tell rails which attribute to use... I suppose you could mod the logic for find to include your syntax... -- Posted via http://www.ruby-forum.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.
Ar Chron wrote:> Pito Salas wrote: >> Here''s an example: >> >> class LineItem < ActiveRecord::Base >> belongs_to :order >> end >> >> class Order < ActiveRecord::Base >> has_many :line_items >> end >> >> my_line_item = LineItem.find(:zipcode => "12345") >> >> Order.find_by_line_item(my_line_item) # doesn''t work >> >> Order.find_by_line_item_id (my_line_item) # does work >> > > Probably not explaining this clearly enough (I blame the distillery > masters at Lagavulin), but:Wish I was there! Which of their Beer is available in the US?> > find_by_line_item fails - my_line_item is an instance of the class (an > object) that does not have a ''line_item'' attribute you indicated, it > *is* a LineItemIt kind of does, in that Order has_many :line_items, no? And so that''s an ActiveRecord method on Order called line_items? By the way, my_order.find_by_line_item fails with "no such method", not just by returning []. I wonder if my_order.find_by_line_items would work? -- Posted via http://www.ruby-forum.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.
> > Probably not explaining this clearly enough (I blame thedistillery> > masters at Lagavulin), but: > > Wish I was there! Which of their Beer is available in the US? >Lol, Lagavulin is a famous WHISKY destillery> > > By the way, my_order.find_by_line_item fails with "no such method", not > just by returning []. >ActiveRecord knows that your line_item belongs to an order. So it''s as easy as: my_line_item.Order -- 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.
As I am sure you know the ''find_by*'' methods are all dynamic. They are based on the field/column name, not on the type of object the column is supposed to reference (if any). AR will do the magic of finding the ID value if you pass an object to the method but apparently it will not be "magic enough" to let you remove the ''_id'' suffix from the field/column name in your ''find_by*'' method name. It would be nice if the Rails guys made a little change so we could do what you wanted to do here, though. ;) On Sep 3, 8:27 pm, Pito Salas <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Here''s an example: > > class LineItem < ActiveRecord::Base > belongs_to :order > end > > class Order < ActiveRecord::Base > has_many :line_items > end > > my_line_item = LineItem.find(:zipcode => "12345") > > Order.find_by_line_item(my_line_item) # doesn''t work > > Order.find_by_line_item_id (my_line_item) # does work > > I was surprised by this, because it seems to force me to make reference > to the id field of the line item, rather than referring to the LineItem > as a class or type in the Order.findxxx statement. > > Am I misunderstanding something or missing a trick? > > Thanks!! > > -- Pito > -- > Posted viahttp://www.ruby-forum.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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 4 September 2010 01:27, Pito Salas <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Here''s an example: > > class LineItem < ActiveRecord::Base > belongs_to :order > end > > class Order < ActiveRecord::Base > has_many :line_items > end > > my_line_item = LineItem.find(:zipcode => "12345") > > Order.find_by_line_item(my_line_item) # doesn''t work > > Order.find_by_line_item_id (my_line_item) # does work > > I was surprised by this, because it seems to force me to make reference > to the id field of the line item, rather than referring to the LineItem > as a class or type in the Order.findxxx statement. > > Am I misunderstanding something or missing a trick?I think you are missing a trick. Once you have my_line_item, to get the item''s order just do my_line_item.order There is no need to use find at all. 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.