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

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

--
Killer Ruby PDF Generation named after a magnificent sea creature:
GitHub - practicingruby/prawn: THIS REPOSITORY HAS MOVED TO: | Non-tech stuff at:
http://metametta.blogspot.com

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

···

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/sandal/prawn| Non-tech stuff at:http://metametta.blogspot.com- Hide quoted text -

- Show quoted text -

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

···

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

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

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

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

Cheers

  robert

···

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.

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 Aug 5, 5:30 am, "David A. Black" <dbl...@rubypal.com> wrote:

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.rubypal.comfor details and updates!

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

···

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

On Aug 5, 5:30 am, "David A. Black" <dbl...@rubypal.com> wrote:

I shall investigate :slight_smile:

Jeff

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

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.rubypal.comfor details and updates!

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