# Ruby for Rails

**URL:** <https://www.ruby-forum.com/t/ruby-for-rails/225932>\
**Category:** Rails\
**Created:** [December 14, 2006, 10:02am UTC](https://www.ruby-forum.com/t/ruby-for-rails/225932 "2006-12-14T10:02:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool\_gag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a3d4f5/32.png) [@cool\_gag](https://www.ruby-forum.com/u/cool_gag)\
**Post date:** [December 14, 2006, 10:02am UTC](https://www.ruby-forum.com/t/ruby-for-rails/225932/1 "2006-12-14T10:02:17Z")

</div>

hi friends,

```
  I am new to ruby and i need a help from any one of u.
       I have got four tables and all i have to get the thing is i

```

need to take the Id from one table and pass it in to the another table.  
Similarly in the descending order, one to two, to three, to four. ie ,  
first tables primary key is the second one’s foreign key. second  
table,first table primary key is the third tables primary key and so  
on… Now i have to get that functionality in the manner like, in one  
form i need to get four labels where in i can provide and store in to  
the database… and on submitting, it will lead us to the listing page,  
where in i had maintained drop down list. But the thing is i am getting  
all the contents. I have to get only those first table id contents from  
four tables , through foreign reference.

```
       In the database also it is not passing that id values in to

```

the child tables.

def new  
@country = Country.new  
@state = State.new  
@city = City.new  
@category = Category.new  
end  
def create  
@country=Country.new(params[:country])  
@state=State.new(params[:state])  
#@country.states.create(params[:id])  
@city=City.new(params[:city])  
#@country.cities.create(params[:id])  
#@state.cities.create(params[:id])  
@category=Category.new(params[:category])  
#@country.categories.create(params[:id])  
#@state.categories.create(params[:id])  
#@city.categories.create(params[:id])  
@country.save  
@state.save  
@city.save  
@category.save  
redirect\_to :action =\>‘list’  
end  
def list  
end

and the models are

class Country \< ActiveRecord::Base  
has\_many :states  
has\_many :cities  
end

class State \< ActiveRecord::Base  
belongs\_to :country  
has\_many :cities  
end

class City \< ActiveRecord::Base  
belongs\_to :state  
belongs\_to :country  
end

class Category \< ActiveRecord::Base  
belongs\_to :state  
belongs\_to :country  
belongs\_to : city  
end

and the view files are:

list.rhtml

#### Select Country \<%= select('name', 'id', Country.find\_all.collect {|country| country.name}) %\>

#### Select State \<%= select('name', 'id', State.find\_all.collect {|state| state.name}) %\>

#### Select City \<%= select('name', 'id', City.find\_all.collect {|city| city.name}) %\>

#### Select Category \<%= select('name', 'id', Category.find\_all.collect {|category| category.name}) %\>

new.rhtml

\<%= start\_form\_tag :action =\> ‘create’ %\>

#### Country \<%= text\_field 'country', 'name' %\>

#### State \<%= text\_field 'state', 'name' %\>

#### City \<%= text\_field 'city', 'name' %\>

#### Category \<%= text\_field 'category', 'name' %\> \<%= submit\_tag "Add" %\> \<%= end\_form\_tag %\>

please have a look in to my coding and help me out with some suggestions  
or modifications

Expecting a help from rubians

thank u
