# Problem with id's

**URL:** <https://www.ruby-forum.com/t/problem-with-ids/229126>\
**Category:** Rails\
**Created:** [December 14, 2006, 11:00am UTC](https://www.ruby-forum.com/t/problem-with-ids/229126 "2006-12-14T11:00:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@dilip](https://www.ruby-forum.com/u/dilip)\
**Post date:** [December 14, 2006, 11:00am UTC](https://www.ruby-forum.com/t/problem-with-ids/229126/1 "2006-12-14T11:00:32Z")

</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

---

<div class="post-metadata">

**Author:** ![dilip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@dilip](https://www.ruby-forum.com/u/dilip)\
**Post date:** [December 14, 2006, 11:23am UTC](https://www.ruby-forum.com/t/problem-with-ids/229126/2 "2006-12-14T11:23:22Z")

</div>

I am not sure I follow, but:

There is no :id being passed to the controller from the form.

The create method could be:

def create  
@country=Country.find\_or\_create\_by name(params[:country])

@state=State.find\_or\_create\_by\_name(params[:state])  
@state.country\_id = @country.id  
@state.save

@city=City.create(params[:city])  
@city.country\_id = @country.id  
@city.state\_id = @state.id  
@city.save

@category=Category.find\_or\_create\_by\_name(params[:category])  
@category.country\_id = @country.id  
@category.state\_id = @state.id  
@categort.city\_id = @city.id  
@category.save

redirect\_to :action =\>‘list’

end

Anyone have another way?

---

<div class="post-metadata">

**Author:** ![dilip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@dilip](https://www.ruby-forum.com/u/dilip)\
**Post date:** [December 14, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/problem-with-ids/229126/3 "2006-12-14T20:54:56Z")

</div>

Opps.

Move the SQL logic out of your view - it’s bad practice. Much more  
readable if its in the controller, or even better pushed down to the  
model.

Try this in the list view:

\<%= form\_tag :action =\> “update” do %\>

#### Select Country \<% @countries.each do |country| %\> \<%= select('name', country) %\> \<% end %\>

#### Select State \<% @states.each do |state| %\> \<%= select('name', state) %\> \<% end %\>

#### Select City \<% @cities.each do |city| %\> \<%= select('name', city) %\> \<% end %\>

#### Select Category \<% @categories.each do |category| %\> \<%= select('name', category) %\> \<% end %\>

\<%= submit\_tag %\>

\<%= end %\>

and in the controller:  
def list  
@countries = Country.find(:all)  
@states = State.find(:all)  
@cities = City.find(:all)  
@categories = Category.find(:all)  
end

- The list view code may not work and it’s ugly, but you get the idea.  
Can anyone tell us if you can do something like:

\<%= select(‘name’, @categories) %\>

??

---

<div class="post-metadata">

**Author:** ![dilip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@dilip](https://www.ruby-forum.com/u/dilip)\
**Post date:** [December 14, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/problem-with-ids/229126/4 "2006-12-14T12:05:47Z")

</div>

askegg wrote:

> I am not sure I follow, but:
> 
> There is no :id being passed to the controller from the form.
> 
> The create method could be:
> 
> def create  
> @country=Country.find\_or\_create\_by name(params[:country])
> 
> @state=State.find\_or\_create\_by\_name(params[:state])  
> @state.country\_id = @country.id  
> @state.save
> 
> @city=City.create(params[:city])  
> @city.country\_id = @country.id  
> @city.state\_id = @state.id  
> @city.save
> 
> @category=Category.find\_or\_create\_by\_name(params[:category])  
> @category.country\_id = @country.id  
> @category.state\_id = @state.id  
> @categort.city\_id = @city.id  
> @category.save
> 
> redirect\_to :action =\>‘list’
> 
> end
> 
> Anyone have another way?

hi askegg

we are really thankful for givimg this code. but there was a small  
problem we are getting the things like —!map:  
HashWithIndifferentAccess name:----- for the country ,state and  
category fields only. we are getting the data saved with ids and  
displayed in thew dropdown box with the value in that name: ---- area

---

<div class="post-metadata">

**Author:** ![dilip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@dilip](https://www.ruby-forum.com/u/dilip)\
**Post date:** [December 15, 2006, 6:48am UTC](https://www.ruby-forum.com/t/problem-with-ids/229126/5 "2006-12-15T06:48:42Z")

</div>

Chandoo R. wrote:

> askegg wrote:
> 
> > I am not sure I follow, but:
> > 
> > There is no :id being passed to the controller from the form.
> > 
> > The create method could be:
> > 
> > def create  
> > @country=Country.find\_or\_create\_by name(params[:country])
> > 
> > @state=State.find\_or\_create\_by\_name(params[:state])  
> > @state.country\_id = @country.id  
> > @state.save
> > 
> > @city=City.create(params[:city])  
> > @city.country\_id = @country.id  
> > @city.state\_id = @state.id  
> > @city.save
> > 
> > @category=Category.find\_or\_create\_by\_name(params[:category])  
> > @category.country\_id = @country.id  
> > @category.state\_id = @state.id  
> > @categort.city\_id = @city.id  
> > @category.save
> > 
> > redirect\_to :action =\>‘list’
> > 
> > end
> > 
> > Anyone have another way?  
> > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
> > Thank you daa…

The code u sent worked after some small small modifications.  
I think you may had got the view about what we asked yesterday.  
now i will say it clearly yaaaaaar

## Select Country ------------------- Add Country

## Select State ------------------- Add State

## Select City ------------------- Add City

## Select Category ------------------- Add Category

go

Add New

* * *

## Thats our view page, where in we get the drop down for the selection

The Add New in the bottom, takes us to another page, where in we have  
four text boxes for the four entries.  
The Add country also lead us to the same page again and working fine.

But when we click the link Add State, it should lead us to the page  
where in i have only text boxes for adding state ,city, category. And so  
on for the next two links ie,add city has city, category and add  
category has just category.

Now, we are getting the error like “Called id for nil, which would  
mistakenly be 4 – if you really wanted the id of nil, use object\_id”,  
when we click the link Add states/cities/category. Thats because , we  
are not passing the country id in their

## states.rhtml

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

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

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

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

## cities.rhtml

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

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

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

## Category.rhtml

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

## 

#### Category \<%= text\_field ‘category’, ‘name’ %\> \<%= submit\_tag “Add” %\> \<%= end\_form\_tag %\>

## and in the only controller, we are using some thing like this

def new  
@country = Country.new  
@state = State.new  
@city = City.new  
@category = Category.new  
end  
def create

@country=Country.create(params[:country])

@state=State.create(params[:state])  
@state.country\_id = @country.id  
@state.save

@city=City.create(params[:city])  
@city.country\_id = @country.id  
@city.state\_id = @state.id  
@city.save

@category=Category.create(params[:category])  
@category.country\_id = @country.id  
@category.state\_id = @state.id  
@category.city\_id = @city.id  
@category.save

redirect\_to :action =\>‘list’

## #@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 state @state=State.create(params[:state]) @state.country\_id = @country.id @state.save redirect\_to :action =\>‘list’ end def city @city=City.create(params[:city]) @city.country\_id = @country.id @city.state\_id = @state.id @city.save redirect\_to :action =\>‘list’ end def category @category=Category.create(params[:category]) @category.country\_id = @country.id @category.state\_id = @state.id @category.city\_id = @city.id @category.save redirect\_to :action =\>‘list’ end def list end

Actually we need some thing likethe way, when i select one country, the  
states of that country must be displayed in the states drop down list  
and when i select a state, the cities must be be displayed in its  
respective drop down list of that concerned state and so for the  
categoriy drop down too

Ie, Just like , i should select a country and i should get that  
country’s states in the state dropdown list. I should get that state’s  
city drop down list when i select city, the concerned city’s categories  
in the category drop down list.

And when i Have my country not being present in the country list, i  
should add it through the Add Country link and so on for state,city and  
category.

If the country is present and if the state is not there in the list, i  
should add it through Add State link with the Id of the country selected  
in the Country drop down list and so on…

Thats the total view we have to provide my dear.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

---

<div class="post-metadata">

**Author:** ![dilip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/47e85d/32.png) [@dilip](https://www.ruby-forum.com/u/dilip)\
**Post date:** [December 18, 2006, 3:07am UTC](https://www.ruby-forum.com/t/problem-with-ids/229126/6 "2006-12-18T03:07:53Z")

</div>

OK, so you have the same basic problem iterated a few times.

I began to type a few answers to this problem, but kept coming into  
trouble.

Example 1 - Creating a state:

def state  
@state=State.create(params[:state])  
@state.country\_id = @country.id  
@state.save  
redirect\_to :action =\>‘list’  
end

Should be:

def state  
@state=State.create(params[:state])  
@state.country\_id = Country.find\_by\_name(params[:country]).id  
@state.save  
redirect\_to :action =\>‘list’  
end

BUT when the final form is submitted it will create YET ANOTHER state  
in the table…

OK, how about we create some dynamic drop down boxes that change  
content based on previous selections?  
Doable, but a little messy and probably a bit too much effort at this  
point.

What about this? In your main form have a drop down list _and_ a text  
field.  
The user can either select the country/state/city/category from the  
list, and if they do not find it can type one in.  
Then check for the values submitted and act appropriately:  
no drop down selected, no text = throw an error  
drop down, no text = use drop down value  
drop down + text = text overrides dropdown(?? or vice versa?) \<= User  
must be informed prior  
no drop down + text = Use text.

Along the right track?

On Dec 15, 4:48 pm, Chandoo R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
