I am still using Rails 1.0...
I have a simple has_many association:
class Round < ActiveRecord::Base
has_many :scores
end
class Score < ActiveRecord::Base
belongs_to: round
end
When I do something like this (contrived for the sake of clarity):
rr = Round.find(params[:id])
rr.scores.each { |s|
logger.debug("object_id=#{s.round.object_id}")
}
I see a database query on the "rounds" table with each iteration of
the
loop. I also see different instances of "Round" objects being created
for each call to "s.round"
Based on the docs, I guess this is the intended behavior but I am
curious why calls to "s.round" don''t just refer to the parent
object
"rr". In this particular case, the parent object is right there for
the
taking since the child objects were fetched through it. It also seems
like the intuitive thing to do and would certainly help with
performance. So what am I missing?
I want to make sure that I understand this before I go ahead and rip
apart my model (or implement some sort of hack) to accomodate this
behavior. Speaking of which, is there a recommended way to deal with
this? I could see how eager loading might help but, that seems
heavy-handed and unecessary given that I have the parent object right
there anyway. Also, I''d like all of the child "Score" objects
to refer
to the same instance of the parant "Round" object.
Thanks for reading this far.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---