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/associations | |
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/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/join_dependency/join_association.rb | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index 7fa0844a5e..ae1b38ff86 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -55,7 +55,12 @@ module ActiveRecord def find_parent_in(other_join_dependency) other_join_dependency.join_parts.detect do |join_part| - parent == join_part + case parent + when JoinBase + parent.base_klass == join_part.base_klass + else + parent == join_part + end end end |