Still missing the boat on class/instance methods

L

laredotornado

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
 
G

Gregory Brown

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
 
L

laredotornado

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

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

Robert Klemme

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
 
D

David A. Black

Hi --

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
 
R

Robert Klemme

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
 
J

Jeff

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
 
F

Frederick Cheung

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
 
D

David A. Black

Hi --

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top