# Bug in rails?

**URL:** <https://www.ruby-forum.com/t/bug-in-rails/94417>\
**Category:** Rails\
**Created:** [May 3, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/bug-in-rails/94417 "2006-05-03T19:02:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [May 3, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/bug-in-rails/94417/1 "2006-05-03T19:02:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [May 3, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/bug-in-rails/94417/2 "2006-05-03T19:02:18Z")

</div>

Find is an active record method that is part of the rails frame work,  
detect is part of Ruby.

[http://api.rubyonrails.com](http://api.rubyonrails.com)

~ Ben

On 5/2/06, David S. [removed\_email\_address@domain.invalid](mailto: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](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Ben R.  
303-947-0446  
[http://www.benr75.com](http://www.benr75.com)

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [May 3, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/bug-in-rails/94417/3 "2006-05-03T19:05:44Z")

</div>

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:

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

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

```
