Forum: Ruby on Rails Bug in rails ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David S. (Guest)
on 2006-05-03 21:02
(Received via mailing list)
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.
Ben R. (Guest)
on 2006-05-03 21:02
(Received via mailing list)
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. <removed_email_address@domain.invalid> wrote:
> current_item = @invoice.line_items.find {|i| i.product.id == key}
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Ben R.
303-947-0446
http://www.benr75.com
Jean-François (Guest)
on 2006-05-03 21:05
(Received via mailing list)
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:
http://api.rubyonrails.org/classes/ActiveRecord/As...

"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.
This topic is locked and can not be replied to.