diff options
author | Jared Armstrong and Neeraj Singh <neerajdotname@gmail.com> | 2013-04-10 08:34:47 -0400 |
---|---|---|
committer | Neeraj Singh <neerajdotname@gmail.com> | 2013-04-10 12:19:47 -0400 |
commit | dc764fcc348d562376add26ff8ef5173946b575b (patch) | |
tree | 3014a67245025f0a5b46a19eeca2fca10a3dee65 /activerecord/lib/active_record/translation.rb | |
parent | bb61d2defab7017807c310ac221c9e69d7f99d96 (diff) | |
download | rails-dc764fcc348d562376add26ff8ef5173946b575b.tar.gz rails-dc764fcc348d562376add26ff8ef5173946b575b.tar.bz2 rails-dc764fcc348d562376add26ff8ef5173946b575b.zip |
While merging relations preserve context for joins
Fixes #3002. Also see #5494.
```
class Comment < ActiveRecord::Base
belongs_to :post
end
class Author < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :author
has_many :comments
end
```
`Comment.joins(:post).merge(Post.joins(:author).merge(Author.where(:name => "Joe Blogs"))).all` would
fail with `ActiveRecord::ConfigurationError: Association named 'author' was not found on Comment`.
It is failing because `all` is being called on relation which looks like this after all the merging:
`{:joins=>[:post, :author], :where=>[#<Arel::Nodes::Equality: ....}`. In this relation all the context that
`Post` was joined with `Author` is lost and hence the error that `author` was not found on `Comment`.
Ths solution is to build JoinAssociation when two relations with join information are being merged. And later
while building the arel use the previously built `JoinAssociation` record in `JoinDependency#graft` to
build the right from clause.
Thanks to Jared Armstrong (https://github.com/armstrjare) for most of the work. I ported it to make it
compatible with new code base.
Diffstat (limited to 'activerecord/lib/active_record/translation.rb')
0 files changed, 0 insertions, 0 deletions