# ActiveRecord and foreign Keys

**URL:** <https://www.ruby-forum.com/t/activerecord-and-foreign-keys/66672>\
**Category:** Rails\
**Created:** [August 28, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/activerecord-and-foreign-keys/66672 "2006-08-28T12:44:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [August 28, 2006, 12:44pm UTC](https://www.ruby-forum.com/t/activerecord-and-foreign-keys/66672/1 "2006-08-28T12:44:24Z")

</div>

Guys,

I have a question about ActiveRecord and how it handles foreign keys.

Say I have two tables, listings and categories:

create table categories (id integer not null auto\_increment primary  
key,  
category varchar(100) not  
null);

create table listings (id integer not null auto\_increment primary key,  
title varchar(255) not null,  
category\_id integer not null);

alter table listings add constraint listings\_categories\_fk foreign key  
(category\_id) references categories (id);

To access these, I create two classes:

class Listing \< ActiveRecord::Base  
belongs\_to :category  
end

class Category \< ActiveRecord::Base  
has\_many :listings  
end

Now if I run some code like:

ActiveRecord::Base.logger = Logger.new("/home/sodonnel/rails/test.log")  
mylisting = Listing.find(1)

puts “current listing is #{mylisting.title}\n”  
puts “its title is #{mylisting.category.category}”

and look in the log file, I can see ActiveRecord required two SQL  
statements to get my details.

If I want to display a list of 50 listings, as Category|Title, it will  
require 51 SQL statements to get me the results.

Is there a better way to get this results set using ActiveRecords built  
in capabilities (ie one that will join the two tables), or do I need to  
‘find\_by\_sql’ or something similar to get this?

Thanks Stephen.

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [August 28, 2006, 12:52pm UTC](https://www.ruby-forum.com/t/activerecord-and-foreign-keys/66672/2 "2006-08-28T12:52:25Z")

</div>

use the :include option for the find method

Listing.find(1, :include =\> :category)

Chris

---

<div class="post-metadata">

**Author:** ![stephen\_OSD](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/df788c/32.png) [@stephen\_OSD](https://www.ruby-forum.com/u/stephen_OSD)\
**Post date:** [August 28, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/activerecord-and-foreign-keys/66672/3 "2006-08-28T13:08:29Z")

</div>

Chris H. wrote:

> > I have a question about ActiveRecord and how it handles foreign keys.  
> > category\_id integer not null);  
> > class Category \< ActiveRecord::Base  
> > puts “its title is #{mylisting.category.category}”
> > 
> > Thanks Stephen.
> > 
> > >

Excellent, thats what I was looking for!
