# When called with <tt>:fill_target => name</tt> it will fill
targets in each object in association
# E.g.: when Author <tt>has_many :posts, :fill_target =>
"author"</tt>
# then <tt>some_author.posts.each {|m| m.author.name }</tt> will
not require aditional db queries to fetch author for each post
# (it will use the +some_author+ already from memory)
This mailing list is meant for discussion about implementation details of Rails only. You can post support requests to Rails Talk [1] or #rubyonrails on freenode.
Sorry, but I thought it is about Rails implementation details,
has_many implementation details.
This mailing list is meant for discussion about implementation
details of Rails only. You can post support requests to Rails Talk
[1] or #rubyonrails on freenode.
> Why has_many doesn't set target for objects in its collection?
> E.g. when
> class Author < ActiveRecord; has_many :posts; end
> class Post < ActiveRecord; belongs_to :author; end
> then
> some_author.posts.each {|p| puts p.author.name}
> will require additional queries to fetch author for each post.
That said, some_author.posts.find(:all, :include => :author).each { |
p> puts p.author.name } will do a JOIN for you. Although that
shouldn't be necessary if you put some_author in an instance variable
and read it from there.
Manfred
That said, :include is not a solution, cause it also unnecessarily
makes bigger query just to fetch object which I already have in
memory.
Instant variable is not enough. My example was intentionally simple
just to show what I mean.
Imagine that I have few authors somewhere, and I collect their posts,
and this posts list is accessed from outside.
Also I was more hoping to see opinion about suggested extension to
has_many.
That said, :include is not a solution, cause it also unnecessarily
makes bigger query just to fetch object which I already have in
memory.
Instant variable is not enough. My example was intentionally simple
just to show what I mean.
Imagine that I have few authors somewhere, and I collect their posts,
and this posts list is accessed from outside.
Also I was more hoping to see opinion about suggested extension to
has_many.
ActiveRecord on edge rails has a cache that is active in
ActionController requests so this shouldn't be a problem. I also have
a plugin that takes a slightly different route in caching frequently
accessed records in a single request.
It's definitely possible to do some form of bidirectional association
at present. You just need to either calculate or configure the
belongs_to association which is the inverse of the has_many.
For some simple cases calculation could work. However it starts to
get quite tricky when the has_many has :conditions or :includes
applied, or if there are multiple has_many associations etc.
However if you whip up a plugin or patch which implements this, I'm
sure there are people who'd help out with testing it. It's a
surprising gotcha for some users.
Having said that, rick's context plugin implements an identity map, so
it could be worth investigating first.