ActiveRecord and foreign Keys.

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.

use the :include option for the find method

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

Chris

Chris Hall wrote:

use the :include option for the find method

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

Chris

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

Excellent, thats what I was looking for!