Still missing the boat on class/instance methods

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

I intended to create an instance method. But then, in a view, when I
call

<%= check_box_tag(‘form[form_items][]’, form_item.id,
@ec_order.has_item(form_item.id)) %>

I get the error, “undefined method `has_item’ for #<EcOrder:
0xb788e264>”.

Sorry to make you guys repeat yourselves, but I think I’m really
close, - Dave

On Mon, Aug 4, 2008 at 4:55 PM, laredotornado
[email protected] wrote:

  ...
   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

You defined an attribute writer but I think you want a conditional
check.
Note that ‘this’ is not valid ruby syntax, you’re looking for self,
but it’s usually not necessary.

Using pure Ruby:

def has_item?(item_id)
ec_line_items.any? { |e| e.form_item_id == item_id }
end

But you probably want to do that check using ActiveRecord, probably a
find with some conditions.
That’s Rails stuff though, and would much better be handled over on
the Ruby on Rails - Talk mailing list.

-greg

On Aug 4, 4:09 pm, Gregory B. [email protected] wrote:

end

  • Show quoted text -
    Thanks for cleaning up my poor syntax. That works brilliantly, - Dave

Hi –

On Tue, 5 Aug 2008, Robert K. wrote:

   has_many :ec_line_items
           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

On 05.08.2008 12:30, David A. Black wrote:

On Tue, 5 Aug 2008, Robert K. wrote:

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.

I knew there would be some trouble (hence the “probably” above). Thank
you for pointing it out.

Cheers

robert

2008/8/5 laredotornado [email protected]:

   validates_associated :ec_line_items
   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

Kind regards

robert

On Aug 5, 5:30 am, “David A. Black” [email protected] wrote:

ActiveRecord overrides #find for its association collections, so you’d
have to use #detect, or a :conditions clause on the find.

It’s one of the few things that make me cringe about Rails - some
native Ruby things are changed when they shouldn’t be.

I wonder if I could submit a patch to make #find, when given only a
block, fall back to “normal” Ruby usage.

I shall investigate :slight_smile:

Jeff

REST with Rails, Oct 4, 2008, in Austin, TX:
http://www.purpleworkshops.com/workshops/rest-and-web-services

On 8 Aug 2008, at 15:42, Jeff wrote:

On Aug 5, 5:30 am, “David A. Black” [email protected] wrote:

ActiveRecord overrides #find for its association collections, so
you’d
have to use #detect, or a :conditions clause on the find.

It’s one of the few things that make me cringe about Rails - some
native Ruby things are changed when they shouldn’t be.

The association proxy isn’t an Array, so an association proxy having
it’s own find is no different that one of your classes implemeneting a
[] method

I wonder if I could submit a patch to make #find, when given only a
block, fall back to “normal” Ruby usage.

One way out is some_activerecord_object.some_collection.to_a.find

Fred

Hi –

On Sat, 9 Aug 2008, Frederick C. wrote:

The association proxy isn’t an Array, so an association proxy having it’s own
find is no different that one of your classes implemeneting a [] method

I wonder if I could submit a patch to make #find, when given only a
block, fall back to “normal” Ruby usage.

One way out is some_activerecord_object.some_collection.to_a.find

Also, #detect is not overridden, so you can do:

obj.some_collection.detect {…}

David