# Automatic finder

**URL:** <https://www.ruby-forum.com/t/automatic-finder/57703>\
**Category:** Rails\
**Created:** [April 13, 2006, 4:46am UTC](https://www.ruby-forum.com/t/automatic-finder/57703 "2006-04-13T04:46:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [April 13, 2006, 4:46am UTC](https://www.ruby-forum.com/t/automatic-finder/57703/1 "2006-04-13T04:46:57Z")

</div>

It looks rails automatically creates a “finder” method on models that  
have a “belongs\_to”. So I tried this in the console:

c = Category.find(2)  
chunks = Chunk.find\_by\_category(c)

chunks is always returned as an empty array. When I know have some  
chunks with a category\_id of 2.

Should the find\_by\_category do what I think it should be doing or am I  
completely off base?

thanks, cott.

* * *

What’s an Intel chip doing in a Mac? A whole lor more that it’s ever  
done in a PC.

My Digital Life - [http://scottwalter.com/blog](http://scottwalter.com/blog)  
Pro:Blog - [http://scottwalter.com/problog](http://scottwalter.com/problog)

---

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [April 13, 2006, 7:25am UTC](https://www.ruby-forum.com/t/automatic-finder/57703/2 "2006-04-13T07:25:04Z")

</div>

First of all the find\_by\_xxx methods just return 1 record. I think the  
find\_all\_by\_xxx is what you need.

Does this work?  
chunks = Chunk.find\_all\_by\_category\_id(2)

I’m not entirely sure the dynamic finders work with associations, but  
since “category\_id” is a direct property of the model it shouldn’t have  
a problem with it. Might make a nice patch though.

-Alex

---

<div class="post-metadata">

**Author:** ![sfwalter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@sfwalter](https://www.ruby-forum.com/u/sfwalter)\
**Post date:** [April 13, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/automatic-finder/57703/3 "2006-04-13T14:23:33Z")

</div>

yes that worked perfectly! thanks.

* * *

What’s an Intel chip doing in a Mac? A whole lor more that it’s ever  
done in a PC.

My Digital Life - [http://scottwalter.com/blog](http://scottwalter.com/blog)  
Pro:Blog - [http://scottwalter.com/problog](http://scottwalter.com/problog)

----- Original Message ----  
From: Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: removed\_email\_address@domain.invalid  
Sent: Thursday, April 13, 2006 12:23:51 AM  
Subject: Re: [Rails] Automatic finder

```
   First of all the find_by_xxx methods just return 1 record. I 

```

think the find\_all\_by\_xxx is what you need.

Does this work?  
chunks = Chunk.find\_all\_by\_category\_id(2)

I’m not entirely sure the dynamic finders work with associations, but  
since “category\_id” is a direct property of the model it shouldn’t have  
a problem with it. Might make a nice patch though.

-Alex

Scott W. wrote: It looks rails  
automatically creates a “finder” method on models that have a  
“belongs\_to”. So I tried this in the console:

c = Category.find(2)  
chunks = Chunk.find\_by\_category(c)

chunks is always returned as an empty array. When I know have some  
chunks with a category\_id of 2.

Should the find\_by\_category do what I think it should be doing or am I  
completely off base?

thanks, cott.

* * *

What’s an Intel chip doing in a Mac? A whole lor more that it’s ever  
done in a PC.

My Digital Life - [http://scottwalter.com/blog](http://scottwalter.com/blog)  
Pro:Blog - [http://scottwalter.com/problog](http://scottwalter.com/problog)

```
_______________________________________________ Rails mailing list 

```

removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
