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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
use the :include option for the find method Listing.find(1, :include => :category) Chris On 8/28/06, stephen O''D <stephen.odonnell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Chris Hall wrote:> use the :include option for the find method > > Listing.find(1, :include => :category) > > Chris > > On 8/28/06, stephen O''D <stephen.odonnell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---