From: "David A. Black" Date: 2008-08-05T19:30:37+09:00 Subject: Re: Still missing the boat on class/instance methods Hi -- On Tue, 5 Aug 2008, Robert Klemme wrote: > 2008/8/5 laredotornado : >> On Aug 4, 4:09 pm, Gregory Brown wrote: >>> On Mon, Aug 4, 2008 at 4:55 PM,laredotornado wrote: >>>> Hi, >>> >>>> Yesterday the group was kind enough to explain distinctions between >>>> class and instance methods, but I guess I still missed something b/c >>>> I'm getting an error. I have this method in my EcOrder model ... >>> >>>> class EcOrder < ActiveRecord::Base >>>> has_many :ec_line_items >>>> validates_associated :ec_line_items >>> >>>> ... >>>> def has_item=(form_item_id) >>>> for ec_line_item in this.ec_line_items >>>> if ec_line_item.form_item_id == form_item_id >>>> true >>>> return >>>> end >>>> end >>>> false >>>> end >>>> end > > Also you can probably simplify this to > > def has_item? form_item_id > ec_line_items.find {|ec_line_item| ec_line_item.form_item_id == form_item_id} > end ActiveRecord overrides #find for its association collections, so you'd have to use #detect, or a :conditions clause on the find. David -- Rails training from David A. Black and Ruby Power and Light: * Advancing With Rails August 18-21 Edison, NJ * Co-taught by D.A. Black and Erik Kastner See http://www.rubypal.com for details and updates!