[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Still missing the boat on class/instance methods

(D. Alvarado)

8/4/2008 8:54:00 PM

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
8 Answers

Gregory Brown

8/4/2008 9:09:00 PM

0

On Mon, Aug 4, 2008 at 4:55 PM, laredotornado <laredotornado@zipmail.com> 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

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

--
Killer Ruby PDF Generation named after a magnificent sea creature:
http://github.com/sa... | Non-tech stuff at:
http://metametta.bl...

(D. Alvarado)

8/4/2008 11:03:00 PM

0

On Aug 4, 4:09 pm, Gregory Brown <gregory.t.br...@gmail.com> wrote:
> On Mon, Aug 4, 2008 at 4:55 PM,laredotornado<laredotorn...@zipmail.com> 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
>
> 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
>
> --
> Killer Ruby PDF Generation named after a magnificent sea creature:http://github.com/sa...| Non-tech stuff at:http://metametta.blo... Hide quoted text -
>
> - Show quoted text -

Thanks for cleaning up my poor syntax. That works brilliantly, - Dave

Robert Klemme

8/5/2008 9:10:00 AM

0

2008/8/5 laredotornado <laredotornado@zipmail.com>:
> On Aug 4, 4:09 pm, Gregory Brown <gregory.t.br...@gmail.com> wrote:
>> On Mon, Aug 4, 2008 at 4:55 PM,laredotornado<laredotorn...@zipmail.com> 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

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

David A. Black

8/5/2008 10:31:00 AM

0

Hi --

On Tue, 5 Aug 2008, Robert Klemme wrote:

> 2008/8/5 laredotornado <laredotornado@zipmail.com>:
>> On Aug 4, 4:09 pm, Gregory Brown <gregory.t.br...@gmail.com> wrote:
>>> On Mon, Aug 4, 2008 at 4:55 PM,laredotornado<laredotorn...@zipmail.com> 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.r... for details and updates!

Robert Klemme

8/5/2008 7:33:00 PM

0

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

> On Tue, 5 Aug 2008, Robert Klemme 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

Jeff

8/8/2008 2:42:00 PM

0

On Aug 5, 5:30=A0am, "David A. Black" <dbl...@rubypal.com> 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-we...

> David
>
> --
> Rails training from David A. Black and Ruby Power and Light:
> =A0 * =A0Advancing With Rails =A0 =A0August 18-21 =A0 =A0Edison, NJ
> =A0 * Co-taught by D.A. Black and Erik Kastner
> Seehttp://www.ruby... details and updates!

Frederick Cheung

8/8/2008 3:46:00 PM

0


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

> On Aug 5, 5:30 am, "David A. Black" <dbl...@rubypal.com> 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
>
>
> I shall investigate :-)
>
> Jeff
>
> REST with Rails, Oct 4, 2008, in Austin, TX:
> http://www.purpleworkshops.com/workshops/rest-and-we...
>
>> 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
>> Seehttp://www.ruby... details and updates!
>


David A. Black

8/8/2008 5:37:00 PM

0

Hi --

On Sat, 9 Aug 2008, Frederick Cheung wrote:

>
> On 8 Aug 2008, at 15:42, Jeff wrote:
>
>> On Aug 5, 5:30 am, "David A. Black" <dbl...@rubypal.com> 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

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

obj.some_collection.detect {...}


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.r... for details and updates!