# Iterating on a child table (with scope)

**URL:** <https://www.ruby-forum.com/t/iterating-on-a-child-table-with-scope/137906>\
**Category:** Rails\
**Created:** [May 18, 2008, 1:55am UTC](https://www.ruby-forum.com/t/iterating-on-a-child-table-with-scope/137906 "2008-05-18T01:55:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradleybennett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a88e57/32.png) [@bradleybennett](https://www.ruby-forum.com/u/bradleybennett)\
**Post date:** [May 18, 2008, 1:55am UTC](https://www.ruby-forum.com/t/iterating-on-a-child-table-with-scope/137906/1 "2008-05-18T01:55:15Z")

</div>

I’m having trouble figuring out how to iterate on entries from a table  
that is not the master table where I have my read\_scope defined.  
Example:

Say I want to list all of the birds that exist in all of the cages  
within a certain pet store. I have the read\_scope defined and working  
elsewhere that will limit the scope of my query to just one pet store.  
This is important to note, because the petstore\_id is only available in  
the ‘cages’ table, not in the birds table. So, in order to ensure my  
read\_scope lists only the birds from the one pet store, I’m pretty sure  
that I have to setup my read\_scope as follows, then query for all cages  
and include the related birds.

```
Cage.with_scope(read_scope Cage.table_name) do
    @cages = Cage.paginate(
                      :include => :birds
                      )
end

```

Now, it is in the list view where I am having my problem. I can’t get  
the right syntax of how to iterate through all of the birds in a single  
list. Here are a couple of things I have tried

```
 @cages.each do { |b|
      b.bird.each {}
      }

```

or

```
 @cages.bird.each do |b|

```

…and many others. Just so it is clear, I can’t query directly for the  
bird because there is no petstore\_id and thus the read\_scope setup  
throughout the application does not work. Can anyone suggest a syntax  
that will work?

Thanks,

Brad
