# Find\_by\_parent\_id

**URL:** <https://www.ruby-forum.com/t/find-by-parent-id/107300>\
**Category:** Rails\
**Created:** [June 20, 2007, 10:08am UTC](https://www.ruby-forum.com/t/find-by-parent-id/107300 "2007-06-20T10:08:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [June 20, 2007, 10:08am UTC](https://www.ruby-forum.com/t/find-by-parent-id/107300/1 "2007-06-20T10:08:45Z")

</div>

I guess it will return all the objects whose parent\_id is assigned. But  
I  
get only an object.

I have to do like these

```
@parent_id = params[:parent_id]
if @parent_id.nil?
  conditions = ["parent_id is null"]
else
  conditions = ["parent_id = ?", @parent_id]
end
@categories = Category.find(:all, :conditions => conditions)

```

But I expected that can be done like this  
@categories = Category.find\_by\_parent\_id(params[:parent\_id]) #  
only  
one object

Does anybody have good idea?

–

> **[CodeLineStatistics](https://sourceforge.net/projects/mycodeline/)**
>
> Download CodeLineStatistics for free. The code line statistics takes files and directories from GUI, counts the total files, total sizes of files, total lines, lines of codes, lines of comments and lines of blanks in the files, displays the results...

---

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [June 20, 2007, 10:55am UTC](https://www.ruby-forum.com/t/find-by-parent-id/107300/2 "2007-06-20T10:55:53Z")

</div>

huang zhimin wrote:

> ```
> end
> @categories = Category.find(:all, :conditions => conditions)
> 
> ```
> 
> But I expected that can be done like this  
> @categories = Category.find\_by\_parent\_id(params[:parent\_id]) #  
> only one object
> 
> Does anybody have good idea?

Perhaps you’re looking for find\_all\_by\_parent\_id?

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![huang\_zhimin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@huang\_zhimin](https://www.ruby-forum.com/u/huang_zhimin)\
**Post date:** [June 20, 2007, 12:26pm UTC](https://www.ruby-forum.com/t/find-by-parent-id/107300/3 "2007-06-20T12:26:07Z")

</div>

Thanks

On 6/20/07, Jacob A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > conditions = ["parent_id is null"]
> > 
> > ```
> 
> Perhaps you’re looking for find\_all\_by\_parent\_id?
> 
> –  
> Cheers,
> 
> - Jacob A.
> 
> >

–

> **[CodeLineStatistics](https://sourceforge.net/projects/mycodeline/)**
>
> Download CodeLineStatistics for free. The code line statistics takes files and directories from GUI, counts the total files, total sizes of files, total lines, lines of codes, lines of comments and lines of blanks in the files, displays the results...
