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