mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixed that has_many :through would ignore the hash conditions (closes #11447) [miloops]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9110 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
533da24bbc
commit
bdd88810c1
4 changed files with 26 additions and 2 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Fixed that has_many :through would ignore the hash conditions #11447 [miloops]
|
||||
|
||||
* Fix issue where the :uniq option of a has_many :through association is ignored when find(:all) is called. Closes #9407 [cavalle]
|
||||
|
||||
* Fix duplicate table alias error when including an association with a has_many :through association on the same join table. Closes #7310 [cavalle]
|
||||
|
|
|
@ -286,23 +286,35 @@ module ActiveRecord
|
|||
|
||||
def build_conditions
|
||||
association_conditions = @reflection.options[:conditions]
|
||||
through_conditions = @reflection.through_reflection.options[:conditions]
|
||||
through_conditions = build_through_conditions
|
||||
source_conditions = @reflection.source_reflection.options[:conditions]
|
||||
uses_sti = !@reflection.through_reflection.klass.descends_from_active_record?
|
||||
|
||||
if association_conditions || through_conditions || source_conditions || uses_sti
|
||||
all = []
|
||||
|
||||
[association_conditions, through_conditions, source_conditions].each do |conditions|
|
||||
[association_conditions, source_conditions].each do |conditions|
|
||||
all << interpolate_sql(sanitize_sql(conditions)) if conditions
|
||||
end
|
||||
|
||||
all << through_conditions if through_conditions
|
||||
all << build_sti_condition if uses_sti
|
||||
|
||||
all.map { |sql| "(#{sql})" } * ' AND '
|
||||
end
|
||||
end
|
||||
|
||||
def build_through_conditions
|
||||
conditions = @reflection.through_reflection.options[:conditions]
|
||||
if conditions.is_a?(Hash)
|
||||
interpolate_sql(sanitize_sql(conditions)).gsub(
|
||||
@reflection.quoted_table_name,
|
||||
@reflection.through_reflection.quoted_table_name)
|
||||
elsif conditions
|
||||
interpolate_sql(sanitize_sql(conditions))
|
||||
end
|
||||
end
|
||||
|
||||
def build_sti_condition
|
||||
"#{@reflection.through_reflection.quoted_table_name}.#{@reflection.through_reflection.klass.inheritance_column} = #{@reflection.klass.quote_value(@reflection.through_reflection.klass.name.demodulize)}"
|
||||
end
|
||||
|
|
|
@ -1324,6 +1324,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
assert_equal 2, people(:michael).posts.find(:all, :include => :people).length
|
||||
end
|
||||
|
||||
def test_has_many_through_respects_hash_conditions
|
||||
assert_equal authors(:david).hello_posts, authors(:david).hello_posts_with_hash_conditions
|
||||
assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class BelongsToAssociationsTest < ActiveRecord::TestCase
|
||||
|
|
|
@ -35,6 +35,11 @@ class Author < ActiveRecord::Base
|
|||
has_many :hello_post_comments, :through => :hello_posts, :source => :comments
|
||||
has_many :posts_with_no_comments, :class_name => 'Post', :conditions => 'comments.id is null', :include => :comments
|
||||
|
||||
has_many :hello_posts_with_hash_conditions, :class_name => "Post",
|
||||
:conditions => {:body => 'hello'}
|
||||
has_many :hello_post_comments_with_hash_conditions, :through =>
|
||||
:hello_posts_with_hash_conditions, :source => :comments
|
||||
|
||||
has_many :other_posts, :class_name => "Post"
|
||||
has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
|
||||
:after_add => :log_after_adding,
|
||||
|
|
Loading…
Reference in a new issue