Hi !
I just have a simple question.
I am writing an app using rails to familiarize myself with the framework
(which is pretty cool, by the way).
I just wanted to know why these two work differently (since “find”
should be a synonym for “detect” according to the Pick Axe) :
current_item = @invoice.line_items.detect {|i| i.product.id == key}
current_item = @invoice.line_items.find {|i| i.product.id == key}
(The details of my project are irrelevant, I think.) The first one
works, the second doesn’t. With the “find” variant, I get this error :
“Couldn’t find LineItem without an ID”
It seems as though rails is trying to work with an ActiveRecord. Is this
normal, or is it a bug ?
By the way, could someone point me to somewhere I can find out about the
“can’t modify frozen hash error” (or explain it) ?
David S.
Find is an active record method that is part of the rails frame work,
detect is part of Ruby.
http://api.rubyonrails.com
~ Ben
On 5/2/06, David S. [email protected] wrote:
current_item = @invoice.line_items.find {|i| i.product.id == key}
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
–
Ben R.
303-947-0446
http://www.benr75.com
Hello David,
“Couldn’t find LineItem without an ID”
It seems as though rails is trying to work with an ActiveRecord. Is this
normal, or is it a bug ?
with a :has_many association, your collection gets additional
methods :
See:
“collection.find - finds an associated object according to the same
rules as Base.find.”
"Firm#clients.find (similar to Client.find(id, :conditions => “firm_id
= #{id}”)) "
By the way, if a method doesn’t use a block passed during the call,
Ruby doesn’t bother :
irb(main):001:0> def foo; puts “hello world”;end
=> nil
irb(main):002:0> foo { puts “hello sky” }
hello world
=> nil
By the way, could someone point me to somewhere I can find out
about the “can’t modify frozen hash error” (or explain it) ?
If an object is frozen, you can’t modify its attributes, the object
becomes
immutable. So for an hash :
irb(main):003:0> h = { :foo => :bar }
=> {:foo=>:bar}
irb(main):004:0> h.freeze
=> {:foo=>:bar}
irb(main):005:0> h[:ga] = :bu
TypeError: can’t modify frozen hash
from (irb):5:in `[]=’
from (irb):5
from :0
-- Jean-François.