# Passing hash from controller to view and pluralization?

**URL:** <https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085>\
**Category:** Rails\
**Created:** [August 10, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085 "2006-08-10T14:54:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![baker1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@baker1](https://www.ruby-forum.com/u/baker1)\
**Post date:** [August 10, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085/1 "2006-08-10T14:54:21Z")

</div>

hi,  
i have 2 tables (counties and towns). counties has\_many towns and towns  
belong\_to counties.

now my question:

i thought i would need to do is say @counties = Counties.find(:all).  
should that not return to me all counties in the counties table WITH all  
towns associated with each county?

in my view i was getting error when doing this

if(counties.has\_towns?)

saying undefined has\_towns methods…

another question about plurulization. if i were to say @county vs  
@counties, is there a difference? would RoR assume i just want  
information for 1 county? or does RoR treat it like the same variable?

---

<div class="post-metadata">

**Author:** ![baker1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@baker1](https://www.ruby-forum.com/u/baker1)\
**Post date:** [August 10, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085/2 "2006-08-10T23:10:55Z")

</div>

baker1 wrote:

> in my view i was getting error when doing this
> 
> if(counties.has\_towns?)
> 
> saying undefined has\_towns methods…

If you have “has\_many :towns” in your model definition, than “towns” is  
the methods you use to access the association.

@county.towns #=\> an array of Town objects

If you want to know if this county has any associated towns, remember  
that it returns an array. So you can use any array methods on it.

if @county.towns.any?  
do\_stuff  
end

> another question about plurulization. if i were to say @county vs  
> @counties, is there a difference? would RoR assume i just want  
> information for 1 county? or does RoR treat it like the same variable?

It’s entirely up to you. Rails doesn’t care what you name your  
variables.

def index  
@foo = Something.find(1)  
@bar = Domething.find(:all)  
end

Now from index.rhtml @foo is a single Something object, and @bar is an  
array of Something objects. However convention tell us it’s best to  
names arrays of things plural and single object singular.

---

<div class="post-metadata">

**Author:** ![baker1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@baker1](https://www.ruby-forum.com/u/baker1)\
**Post date:** [August 10, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085/3 "2006-08-10T23:22:15Z")

</div>

hello alex, i really appreciate your help!

if @county.towns contains the towns for the particular county, how can i  
now increment to the next county and show the next set of towns?

basically id like to display something like this

```
for county in @counties
    print county name

    for towns in @counties
        print town name
    end
end

```

is this possible or do i need more than one data container passed to the  
view?

Alex W. wrote:

> baker1 wrote:
> 
> > in my view i was getting error when doing this
> > 
> > if(counties.has\_towns?)
> > 
> > saying undefined has\_towns methods…
> 
> If you have “has\_many :towns” in your model definition, than “towns” is  
> the methods you use to access the association.
> 
> @county.towns #=\> an array of Town objects
> 
> If you want to know if this county has any associated towns, remember  
> that it returns an array. So you can use any array methods on it.
> 
> if @county.towns.any?  
> do\_stuff  
> end
> 
> > another question about plurulization. if i were to say @county vs  
> > @counties, is there a difference? would RoR assume i just want  
> > information for 1 county? or does RoR treat it like the same variable?
> 
> It’s entirely up to you. Rails doesn’t care what you name your  
> variables.
> 
> def index  
> @foo = Something.find(1)  
> @bar = Domething.find(:all)  
> end
> 
> Now from index.rhtml @foo is a single Something object, and @bar is an  
> array of Something objects. However convention tell us it’s best to  
> names arrays of things plural and single object singular.

---

<div class="post-metadata">

**Author:** ![baker1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@baker1](https://www.ruby-forum.com/u/baker1)\
**Post date:** [August 11, 2006, 12:37pm UTC](https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085/4 "2006-08-11T12:37:32Z")

</div>

hey Alex thanks, i will try this when i get home today.

coming from a c++ perspective, i am still having difficulty  
understanding how ruby and rails work. its like what are big no-no’s in  
c++ is a.o.k. in ruby!

a few more questions if you dont mind:

now @counties is the object i declared in my controller that contains  
everything that my mySql counties table has? what is singular ‘county’  
in the for loop? is that the same as saying

for(int i = 0; i \< number of counties in counties table; i++)  
{  
do stuff to each county etc…?

}

Alex W. wrote:

> baker1 wrote:
> 
> > hello alex, i really appreciate your help!
> > 
> > if @county.towns contains the towns for the particular county, how can i  
> > now increment to the next county and show the next set of towns?
> > 
> > basically id like to display something like this
> > 
> > ```
> > for county in @counties
> > print county name
> > 
> > for towns in @counties
> > print town name
> > end
> > end
> > 
> > ```
> > 
> > is this possible or do i need more than one data container passed to the  
> > view?
> 
> you almost have it
> 
> \<% for county in @counties %\>
> 
> # \<%= county.name %\>
> \<% for town in county.towns %\> 
> 
> \<%= town.name %\>
> 
> \<% end %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![baker1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@baker1](https://www.ruby-forum.com/u/baker1)\
**Post date:** [August 10, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085/5 "2006-08-10T23:16:18Z")

</div>

Also, your controller should be

@counties = Counties.find(:all)

Then your view should have

\<% @counties.each do |county| %\>  
\<% if county.towns.any? %\>  
\<%= link\_to county.name, :action =\> ‘show’, :id =\> county %\>  
  
\<% end %\>  
\<% end %\>

Or something similar.

Note that in your example you cant call any model specific methods on  
@counties because it is an array of County objects, not a county object  
itself.

---

<div class="post-metadata">

**Author:** ![baker1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@baker1](https://www.ruby-forum.com/u/baker1)\
**Post date:** [August 15, 2006, 2:16pm UTC](https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085/6 "2006-08-15T14:16:38Z")

</div>

Hi,

Assuming that in your view the only hash you receive is @counties we can  
iterate its members this way:

```
for county in @counties
    print county.name

    for town in county.towns
        print town.name
    end
end

```

–Romeu

-----Mensagem original-----  
De: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] Em nome de baker1  
Enviada em: quinta-feira, 10 de agosto de 2006 17:22  
Para: removed\_email\_address@domain.invalid  
Assunto: [Rails] Re: passing hash from controller to view and  
pluralization?

hello alex, i really appreciate your help!

if @county.towns contains the towns for the particular county, how can i  
now increment to the next county and show the next set of towns?

basically id like to display something like this

```
for county in @counties
    print county name

    for towns in @counties
        print town name
    end
end

```

is this possible or do i need more than one data container passed to the  
view?

Alex W. wrote:

> @county.towns #=\> an array of Town objects
> 
> > information for 1 county? or does RoR treat it like the same variable?  
> > array of Something objects. However convention tell us it’s best to  
> > names arrays of things plural and single object singular.

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

* * *

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

–  
No virus found in this incoming message.  
Checked by AVG Free Edition.  
Version: 7.1.405 / Virus Database: 268.10.8/415 - Release Date: 9/8/2006

–  
No virus found in this outgoing message.  
Checked by AVG Free Edition.  
Version: 7.1.405 / Virus Database: 268.10.9/416 - Release Date:  
10/8/2006

---

<div class="post-metadata">

**Author:** ![baker1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/22d042/32.png) [@baker1](https://www.ruby-forum.com/u/baker1)\
**Post date:** [August 11, 2006, 12:13am UTC](https://www.ruby-forum.com/t/passing-hash-from-controller-to-view-and-pluralization/102085/7 "2006-08-11T00:13:09Z")

</div>

baker1 wrote:

> hello alex, i really appreciate your help!
> 
> if @county.towns contains the towns for the particular county, how can i  
> now increment to the next county and show the next set of towns?
> 
> basically id like to display something like this
> 
> ```
> for county in @counties
> print county name
> 
> for towns in @counties
> print town name
> end
> end
> 
> ```
> 
> is this possible or do i need more than one data container passed to the  
> view?

you almost have it

\<% for county in @counties %\>

# \<%= county.name %\>
 \<% for town in county.towns %\> 

\<%= town.name %\>

 \<% end %\> \<% end %\>
