On 8/8/07, paccator <[EMAIL PROTECTED]> wrote:
>
> 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.
>
>
> Could some option to has_many be added to pre-fill the target (in this
> case set_author_target for each post)?
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.
--
Cheers
Koz
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---