AR Caching and Reflection


#1

When I do this:
term = Term.find(1, :include => :definition)
term.definition.term
Two SQL queries are fired, why?


#2

can you show the 2 queries ?

and did you make sure you defined the has_X belongs_to relationship in
the
model ?


#3

Adam D. wrote:

can you show the 2 queries ?

and did you make sure you defined the has_X belongs_to relationship in
the
model ?

class Term < ActiveRecord::Base
has_one :definition, :dependent => :destroy
end

class Content < ActiveRecord::Base
end

class Definition < Content
belongs_to :term
end

I run this:

term = Term.find(2, :include => :definition)
term.definition.term

Here are the SQL queries from the log (sorry for all the columns gumming
up the join):

Term Load Including Associations (0.032398) SELECT terms.id AS
t0_r0, terms.title AS t0_r1, terms.created_at AS t0_r2,
terms.updated_at AS t0_r3, terms.formatted_title AS t0_r4,
terms.definition_exists AS t0_r5, contents.id AS t1_r0,
contents.article_id AS t1_r1, contents.term_id AS t1_r2,
contents.parsed AS t1_r3, contents.type AS t1_r4,
contents.parsed_at AS t1_r5, contents.created_at AS t1_r6,
contents.updated_at AS t1_r7, contents.author_id AS t1_r8 FROM terms
LEFT OUTER JOIN contents ON contents.term_id = terms.id AND
contents.type = ‘Definition’ WHERE (terms.id = 2)

Term Load (0.000488) SELECT * FROM terms WHERE (terms.id = 2) LIMIT
1


#4

I’m sorry I was unclear. I was only using term.definition.term as a
very basica example. What I’m actually trying to achieve is this:
I grab a bunch of terms and include their definition. I process all of
the terms I grabbed through various functions, some of which take a
definition. In some of those definition handling functions the term is
used. When those fire off they are in effect calling
term.definition.term


#5

i think you are looping here. Are you looking for the term based on a
definition or the definition based on a term ?

you shoudl only need to do “term.definition” and that should give you
the
definition for the term id=2. Or does the definition table have an
actual
field called “term” ? Maybe thats what screwing this up here.

adam


#6

On 5/24/06, John W. removed_email_address@domain.invalid wrote:

I’m sorry I was unclear. I was only using term.definition.term as a
very basica example. What I’m actually trying to achieve is this:
I grab a bunch of terms and include their definition. I process all of
the terms I grabbed through various functions, some of which take a
definition. In some of those definition handling functions the term is
used. When those fire off they are in effect calling
term.definition.term

Term Load Including Associations

term = Term.find(1, :include => :definition)

Term Load

term.definition.term

Even though it’s the same association, it’s not loaded automatically.
One solution is to use cascading eager includes:

Term.find 1, :include => { :definition => :term }

But, I think this is very unnecessary in your case. Why not do this?

defs = Definition.find :all, :include => :term ?

need to query on the terms?

Definition.find :all, :include => :term, :conditions => [‘terms.name =
?’, ‘foo’]
Definition.find :all, :include => :term, :conditions => [‘terms.id in
(?)’, [1,2,3]]

need to pull all the selected terms?

terms = defs.collect { |d| d.term }.uniq

It really helps to pull up the log and script/console so you can work
with your models and see when the database is queried.