# Has many and link\_to

**URL:** <https://www.ruby-forum.com/t/has-many-and-link-to/145091>\
**Category:** Rails\
**Created:** [August 5, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/has-many-and-link-to/145091 "2008-08-05T23:00:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![perezda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@perezda](https://www.ruby-forum.com/u/perezda)\
**Post date:** [August 5, 2008, 11:00pm UTC](https://www.ruby-forum.com/t/has-many-and-link-to/145091/1 "2008-08-05T23:00:49Z")

</div>

Ok so I am building a directory that is organized by state, county,  
city, zip. I created tables for each of them, so i have a table for  
states, counties, cities, and zips. Also in each table I have a title  
column and a corresponding reference \_id table. Meaning in my states  
table i have a title column and a county\_id colomun. same with all the  
other tables.

In my models folder for state.rb I have the following

class State \< ActiveRecord::Base  
has\_many :counties  
end

And in my models folder for county.rb I have the following

class County \< ActiveRecord::Base  
belongs\_to :state  
has\_many :cities  
end

I have also generated a scaffold for state, county, city, and zip, all  
with a title column.

I also modified the new action for each one so I am able to select  
what counties belong to which states, and what cities belong to which  
counties etc.

I created a new display controller so I can view my new directory and  
it starts off with an index file that shows all the states like this

# Listing States
\<% for state in @states %\> \<% end %\>

| \<%= link\_to state.title, {:controller =\> 'display', :action =\> 'counties'} %\> |

And then I have another file in my display folder view called counties  
and it has the following in it.

# Listing counties
\<% for county in @counties %\> \<% end %\>

| \<%= link\_to county.title, {:controller =\> 'display', :action =\> 'cities'} %\> |

And it continues, however when u click on a particular state I only  
want it to show the counties that belongs to it and I do not know how  
to do that. Any help would be greatly appreciated. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![perezda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@perezda](https://www.ruby-forum.com/u/perezda)\
**Post date:** [August 6, 2008, 12:12am UTC](https://www.ruby-forum.com/t/has-many-and-link-to/145091/2 "2008-08-06T00:12:15Z")

</div>

Instead of having a display controller with actions for each of your  
resources, it’s better to have a controller for each of your resources.

map.resources :cities, :states, :counties

link\_to cities\_path

More information can be found here:

> **[frozenplague.net](http://frozenplague.net/2008/01/06/restful-routing-an-overview/)**
>
> This domain may be for sale!

. The tutorial covers restful routing as well as restricting results  
to the current object.

---

<div class="post-metadata">

**Author:** ![perezda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@perezda](https://www.ruby-forum.com/u/perezda)\
**Post date:** [August 6, 2008, 12:30am UTC](https://www.ruby-forum.com/t/has-many-and-link-to/145091/3 "2008-08-06T00:30:22Z")

</div>

Yeah,nested routes can help you out  
When you need a state\_id, just let the user provide it in the URL!

---

<div class="post-metadata">

**Author:** ![perezda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/46a35a/32.png) [@perezda](https://www.ruby-forum.com/u/perezda)\
**Post date:** [August 6, 2008, 2:29am UTC](https://www.ruby-forum.com/t/has-many-and-link-to/145091/4 "2008-08-06T02:29:40Z")

</div>

Thank you for your replies, i checked out that link that ryan bigg  
suggested and it helped out a lot, thanks.
