# Table connection issue

**URL:** <https://www.ruby-forum.com/t/table-connection-issue/195455>\
**Category:** Rails\
**Created:** [September 23, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/table-connection-issue/195455 "2010-09-23T13:01:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 23, 2010, 1:01pm UTC](https://www.ruby-forum.com/t/table-connection-issue/195455/1 "2010-09-23T13:01:50Z")

</div>

Dear all,

I included models, controller and View. Please, find the mistake in it  
and  
tell me. I need to fetch the data of included tables “city and country”  
using the query of “first name and second name” of table ‘name’.

_name model_  
has\_many :city, :foreign\_key =\> “name\_id”  
has\_many :country, :foreign\_key =\> “name\_id”  
\*  
city model\*  
belongs\_to :name

_country model_  
belongs\_to :name

_In Name Controller_  
@names = Name.find(:all, :conditions=\>{:first\_name =\> params[:gm],  
:second\_name =\> params[:sp]}, :include =\> [:city, :country])

_In Name View_

\<% @names.each do |name| %\>  
\<%= name.age %\>  
\<% end %\>

\<% @names.cities.each do |city| %\>  
\<%= city.local\_area %\>  
\<% end %\>

\<% @names.countries.each do |country| %\>  
\<%= country.state %\>  
\<% end %\>

–  
With Regards  
Palani Kannan. K

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 23, 2010, 2:05pm UTC](https://www.ruby-forum.com/t/table-connection-issue/195455/2 "2010-09-23T14:05:38Z")

</div>

On Sep 23, 12:00 pm, PalaniKannan K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dear all,
> 
> I included models, controller and View. Please, find the mistake in it and  
> tell me. I need to fetch the data of included tables “city and country”  
> using the query of “first name and second name” of table ‘name’.

You could start by stating what the problem is rather than leaving  
people to guess.

Fred

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 23, 2010, 5:13pm UTC](https://www.ruby-forum.com/t/table-connection-issue/195455/3 "2010-09-23T17:13:37Z")

</div>

On 23 September 2010 12:00, PalaniKannan K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Dear all,
> 
> I included models, controller and View. Please, find the mistake in it and  
> tell me. I need to fetch the data of included tables “city and country”  
> using the query of “first name and second name” of table ‘name’.
> 
> name model  
> has\_many :city, :foreign\_key =\> “name\_id”  
> has\_many :country, :foreign\_key =\> “name\_id”

That should be :cities and :countries. You don’t need the foreign key  
spec, name\_id is the default.

Colin

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 23, 2010, 5:25pm UTC](https://www.ruby-forum.com/t/table-connection-issue/195455/4 "2010-09-23T17:25:55Z")

</div>

Dear Colin,

Thanks for your correction. The error solved, but its not still showing  
the  
values for

\<%= city.local\_area %\>  
\<%= country.state %\>

On 23 September 2010 17:11, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > has\_many :city, :foreign\_key =\> “name\_id”
> > 
> > ## \<%= name.age %\>
> > 
> > For more options, visit this group at  
> > For more options, visit this group at  
> > [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
With Regards  
Palani Kannan. K

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 23, 2010, 5:36pm UTC](https://www.ruby-forum.com/t/table-connection-issue/195455/5 "2010-09-23T17:36:28Z")

</div>

Hi,

I tried with

_In View_

\<% @names.each do |name| %\>  
\<%= name.age %\>  
\<% name.cities.each do |city| %\>  
\<%= city.local\_area %\>  
\<% name.countries.each do |country| %\>  
\<%= country.state %\>  
\<% end %\>

This shows no error with no output of city.local\_area and country.state.

On 23 September 2010 17:24, PalaniKannan K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > has\_many :city, :foreign\_key =\> “name\_id”
> > > 
> > > ## \<%= name.age %\>
> > 
> > .  
> > .

–  
With Regards  
Palani Kannan. K

---

<div class="post-metadata">

**Author:** ![PalaniKannan\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/eb8c5e/32.png) [@PalaniKannan\_K](https://www.ruby-forum.com/u/PalaniKannan_K)\
**Post date:** [September 23, 2010, 5:42pm UTC](https://www.ruby-forum.com/t/table-connection-issue/195455/6 "2010-09-23T17:42:19Z")

</div>

On 23 September 2010 16:35, PalaniKannan K [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

Please don’t top post it makes it difficult to follow the thread,  
thanks.

> \<% name.countries.each do |country| %\>  
> \<%= country.state %\>  
> \<% end %\>
> 
> This shows no error with no output of city.local\_area and country.state.

Are you sure it is finding any cities and countries? Try displaying  
name.cities.count and the same for countries. Even better have a look  
at the Rails Guide on debugging and use ruby-debug to break in and  
examine the variables.

Colin
