# Show all children of object

**URL:** <https://www.ruby-forum.com/t/show-all-children-of-object/186409>\
**Category:** Rails\
**Created:** [April 6, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/show-all-children-of-object/186409 "2010-04-06T18:30:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 6, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/show-all-children-of-object/186409/1 "2010-04-06T18:30:29Z")

</div>

I have an index page of ‘objects A’ who each have many of another  
‘object B’.

I want to have a link that will open a popup and list all the object  
B’s of an object A.

I have this:

\<%= link\_to( “details of object B”,  
{ :controller=\>“objectA”, :action=\>“showObjectBs”, :ObjectA =\>  
ObjectA}, { :popup=\>[‘ObjectBs’,  
‘height=600,width=650,location=no,scrollbars=yes’]} ) %\>

and then in the action showObjectBs in the Object A controller:

def showObjectBs  
@ObjectBs = ObjectB.find(:all, :conditions =\> [‘objectA\_id’ ==  
@ObjectA.id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @objectB}
end

```

end

but I get an error:

ActiveRecord::RecordNotFound in ObjectAsController#show

Couldn’t find ObjectA with ID=showObjectBs

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 6, 2010, 7:41pm UTC](https://www.ruby-forum.com/t/show-all-children-of-object/186409/2 "2010-04-06T19:41:07Z")

</div>

ES wrote:

> I have an index page of ‘objects A’ who each have many of another  
> ‘object B’.
> 
> I want to have a link that will open a popup and list all the object  
> B’s of an object A.

class A  
has\_many :bs  
end

class B  
belongs\_to :a  
end

(AsController)  
def b\_popup  
@a = A.find(params[:id])  
@bs = @a.bs  
end

(A index view)  
link\_to(‘test’,  
{:controller =\> ‘as’,  
:action =\> ‘b\_popup’,  
:id =\> a.id},  
{:popup =\> [your stuff here]})

make sure your route in the link\_to is available in routes.rb via  
map.resources for your object A

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 6, 2010, 7:48pm UTC](https://www.ruby-forum.com/t/show-all-children-of-object/186409/3 "2010-04-06T19:48:56Z")

</div>

It seems you have 3 problems with your snippets :  
First you need to find objectA in your database to use it  
Then the conditions in the find method doesn’t seem correct  
Finally you’ve forgottent an ‘s’ at the end of @objectB  
(And the naming convention you’re using is not correct but that’s not  
the problem)

So try with this instead :

def showObjectBs  
@ObjectA = ObjectA.find(params[:id])  
@ObjectBs = ObjectB.find(:all, :conditions =\> [‘objectA\_id ?’,  
@ObjectA.id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @objectBs}
end

```

end

Le 06/04/2010 18:29, ES a écrit :

> ObjectA}, { :popup=\>[‘ObjectBs’,  
> format.html # show.html.erb

–  
Aurélien AMILIN

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 6, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/show-all-children-of-object/186409/4 "2010-04-06T21:46:02Z")

</div>

AMILIN AurÃ©lien wrote:

> So try with this instead :
> 
> def showObjectBs  
> @ObjectA = ObjectA.find(params[:id])  
> @ObjectBs = ObjectB.find(:all, :conditions =\> [‘objectA\_id ?’,  
> @ObjectA.id])
> 
> ```
> respond_to do |format|
> format.html # show.html.erb
> format.xml { render :xml => @objectBs}
> end
> 
> ```
> 
> end

You should also fix your variable names. The standard ruby convention  
for variable names is all lower case with underscores separating words.  
Same goes for method names:

def show\_object\_bs  
@object\_a = ObjectA.find(params[:id])  
@object\_bs = ObjectB.find(:all, :conditions =\> [‘object\_a\_id ?’,  
@object\_a.id]  
…  
…  
end

Notice also that class names begin with upper case, and use camel case  
to separate words.
