# Still missing the boat on class/instance methods

**URL:** <https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981>\
**Category:** Ruby\
**Created:** [August 4, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981 "2008-08-04T22:57:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 4, 2008, 10:57pm UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/1 "2008-08-04T22:57:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 4, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/2 "2008-08-04T23:11:08Z")

</div>

On Mon, Aug 4, 2008 at 4:55 PM, laredotornado  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 5, 2008, 1:06am UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/3 "2008-08-05T01:06:25Z")

</div>

On Aug 4, 4:09 pm, Gregory B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 5, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/4 "2008-08-05T12:33:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 5, 2008, 9:35pm UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/5 "2008-08-05T21:35:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 5, 2008, 11:11am UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/6 "2008-08-05T11:11:48Z")

</div>

2008/8/5 laredotornado [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 8, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/7 "2008-08-08T16:44:36Z")

</div>

On Aug 5, 5:30 am, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂

Jeff

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

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 8, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/8 "2008-08-08T17:48:29Z")

</div>

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

> On Aug 5, 5:30 am, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [August 8, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/still-missing-the-boat-on-class-instance-methods/144981/9 "2008-08-08T19:39:43Z")

</div>

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
