# Trying to display data from two tables

**URL:** <https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279>\
**Category:** Rails\
**Created:** [January 4, 2006, 4:25am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279 "2006-01-04T04:25:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 4:25am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/1 "2006-01-04T04:25:34Z")

</div>

Alrighty folks, I’m really trying here, and want to succeed with Ruby on  
Rails.

I’m trying to display data that is linked from two tables using the  
Active Record naming conventions, as described in Agile Web book.

The stripped-down basics;

Database

drop table if exists people;  
CREATE TABLE `people` (  
`id` int(6) NOT NULL auto\_increment,  
`lastname` varchar(30) NOT NULL,  
`personkind_id` int(6) NOT NULL default ‘23’,  
PRIMARY KEY (`id`)  
)

drop table if exists person\_kinds;  
CREATE TABLE `person_kinds` (  
`id` int(6) NOT NULL auto\_increment,  
`kind` varchar(30) NOT NULL,  
PRIMARY KEY (`id`)  
)

###########################

Using ruby script/generate scaffold

Controllers;

class PersonAdminController \< ApplicationController

def list  
@person\_pages, @people = paginate(:people, :order\_by =\>‘lastname’,  
:per\_page =\> 10)  
@person\_kinds = PersonKind.find\_all  
end

class PersonKindAdminController \< ApplicationController

###########################

Models

class Person \< ActiveRecord::Base  
belongs\_to :person\_kind  
end

class PersonKind \< ActiveRecord::Base  
has\_many :people  
end

###########################

list.rhtml

# People by Field

\<% @people.each do |person| %\>

 \<% end %\>

| 

_ **Last Name** _

 | 

_ **Kind** _

 |
| \<%= link\_to person.lastname, :action =\> "show", :id =\> person.id %\> | \<%= person.firstname %\> | \<%= person.person\_kind.kind %\> |

\<%= link\_to "Add New Person", :action =\> "new" %\>

##############################

The \<%= person.person\_kind.kind %\> line in the list.rhtml is what is  
causing my brain to turn to tapioca.

I really thought that that would work. I know this is very basic Rails  
stuff, but I’m stuck. Any help would be greatly appreciated.

Kindest regards.

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 5:16am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/2 "2006-01-04T05:16:17Z")

</div>

Andy P. wrote:

> def list  
> @person\_pages, @people = paginate(:people, :order\_by =\>‘lastname’,  
> :per\_page =\> 10)  
> @person\_kinds = PersonKind.find\_all  
> end

try adding an :include=\>:kind to the paginate function.

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 5:47am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/3 "2006-01-04T05:47:19Z")

</div>

Kevin O. wrote:

> try adding an :include=\>:kind to the paginate function.

Nope it generates the following erorr

“Association was not found; perhaps you misspelled it? You specified  
:include =\> :kind”

If I just use

\<%= person.person\_kind.kind %\>

in the list.rhtml, as in my first post, I get the following erorr,

“You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.kind”

I thought that the belongs\_to & has\_many took care of this.

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 5:50am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/4 "2006-01-04T05:50:41Z")

</div>

It looks like you are missing an underbar. Try ‘person\_kind\_id’ as the  
column name in people.

Andy P. wrote:

> Database
> 
> drop table if exists people;  
> CREATE TABLE `people` (  
> `id` int(6) NOT NULL auto\_increment,  
> `lastname` varchar(30) NOT NULL,  
> `personkind_id` int(6) NOT NULL default ‘23’,  
> PRIMARY KEY (`id`)  
> )
> 
> drop table if exists person\_kinds;  
> CREATE TABLE `person_kinds` (  
> `id` int(6) NOT NULL auto\_increment,  
> `kind` varchar(30) NOT NULL,  
> PRIMARY KEY (`id`)  
> )

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 6:14am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/5 "2006-01-04T06:14:23Z")

</div>

Your code example works because it has nothing to do with how  
ActiveRecord guesses names. AR uses the convention of adding \_id to a  
model name to get the foreign key. On the one hand you have a model  
named “person\_kind”, but on the other you are using the name  
“personkind\_id” for the foreign key. You should have a fk called  
“person\_kind\_id” to follow AR’s convention, or explicitly specify the  
non-standard foreign key in the association method in your model class  
with :foreign\_key =\> “personkind\_id”

Andy P. wrote:  
I thought this as well, however, the following works just fine on my

> edit.rhtml
> 
> \<% @person\_kinds.each do |personkind| %\> \> \<%= personkind.kind %\> \<% end %\>
> 
> Although, I should try ‘person\_kind\_id’ as the column name just to rule  
> it out.

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 6:03am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/6 "2006-01-04T06:03:55Z")

</div>

joshua wrote:

> It looks like you are missing an underbar. Try ‘person\_kind\_id’ as the  
> column name in people.

I thought this as well, however, the following works just fine on my  
edit.rhtml

\<% @person\_kinds.each do |personkind| %\> \> \<%= personkind.kind %\> \<% end %\>

Although, I should try ‘person\_kind\_id’ as the column name just to rule  
it out.

Thanks for taking the time to look at it.

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 6:48am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/7 "2006-01-04T06:48:31Z")

</div>

Andy P. wrote:

> Database
> 
> drop table if exists people;  
> CREATE TABLE `people` (  
> `id` int(6) NOT NULL auto\_increment,  
> `lastname` varchar(30) NOT NULL,  
> `personkind_id` int(6) NOT NULL default ‘23’,  
> PRIMARY KEY (`id`)  
> )
> 
> drop table if exists person\_kinds;  
> CREATE TABLE `person_kinds` (  
> `id` int(6) NOT NULL auto\_increment,  
> `kind` varchar(30) NOT NULL,  
> PRIMARY KEY (`id`)  
> )
> 
> ###########################

I have to agree with Joshua on this. It seems as if the issue lies in  
the foreign key name. I would definitely try renaming ‘personkind\_id’  
to ‘person\_kind\_id’ so that AR can find the link between the two tables.  
Furthermore, check out page 230 in the Agile Web book. It gives some  
good info on how AR actually creates methods for you based on table  
relationships.

So for example, if you used the find() method to find all of the people  
in your DB, you could get the data (attributes) related to each person  
from the other table. I’m not really sure what your app is about but AR  
can handle all of this for you based on the proper naming of the foreign  
key, just double check page 230 in the book.

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 6:53am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/8 "2006-01-04T06:53:45Z")

</div>

richard downe wrote:

> I have to agree with Joshua on this. It seems as if the issue lies in  
> the foreign key name. I would definitely try renaming ‘personkind\_id’  
> to ‘person\_kind\_id’ so that AR can find the link between the two tables.  
> Furthermore, check out page 230 in the Agile Web book. It gives some  
> good info on how AR actually creates methods for you based on table  
> relationships.
> 
> So for example, if you used the find() method to find all of the people  
> in your DB, you could get the data (attributes) related to each person  
> from the other table. I’m not really sure what your app is about but AR  
> can handle all of this for you based on the proper naming of the foreign  
> key, just double check page 230 in the book.

I’ll check page 230 first thing in the morning, thankyou Richard.

I made the assumption that the selection box was using Active Record in  
the same manner.

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 4, 2006, 6:49am UTC](https://www.ruby-forum.com/t/trying-to-display-data-from-two-tables/89279/9 "2006-01-04T06:49:05Z")

</div>

joshua wrote:

> Your code example works because it has nothing to do with how  
> ActiveRecord guesses names. AR uses the convention of adding \_id to a  
> model name to get the foreign key. On the one hand you have a model  
> named “person\_kind”, but on the other you are using the name  
> “personkind\_id” for the foreign key. You should have a fk called  
> “person\_kind\_id” to follow AR’s convention, or explicitly specify the  
> non-standard foreign key in the association method in your model class  
> with :foreign\_key =\> “personkind\_id”

Joshua,

Spot on mate. Thankyou so much.

Changing “personkind\_id” to “person\_kind\_id” solved the problem.

Also, thankyou for taking the time explaining why the selection box  
would work.

Once I made that change, I also had to change my selection box too

\<% @person\_kinds.each do |person\_kind| %\> \> \<%= person\_kind.kind %\> \<% end %\>
