提交 6fbf4082 编写于 作者: R Rick Olson

Allow overriding of find parameters in scoped has_many :through calls [Rick Olson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4007 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 c8470f8a
*SVN*
* Allow overriding of find parameters in scoped has_many :through calls [Rick Olson]
In this example, :include => false disables the default eager association from loading. :select changes the standard
select clause. :joins specifies a join that is added to the end of the has_many :through query.
class Post < ActiveRecord::Base
has_many :tags, :through => :taggings, :include => :tagging do
def add_joins_and_select
find :all, :select => 'tags.*, authors.id as author_id', :include => false,
:joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id'
end
end
end
* Fixed that schema changes while the database was open would break any connections to a SQLite database (now we reconnect if that error is throw) [DHH]
* Don't classify the has_one class when eager loading, it is already singular. Add tests. (closes #4117) [jonathan@bluewire.net.nz]
......
......@@ -24,10 +24,10 @@ def find(*args)
options[:order] = @reflection.options[:order]
end
options[:select] = construct_select
options[:from] = construct_from
options[:joins] = construct_joins
options[:include] ||= @reflection.source_reflection.options[:include]
options[:select] = construct_select(options[:select])
options[:from] ||= construct_from
options[:joins] = construct_joins(options[:joins])
options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil?
merge_options_from_reflection!(options)
......@@ -84,11 +84,11 @@ def construct_from
@reflection.table_name
end
def construct_select
selected = @reflection.options[:select] || "#{@reflection.table_name}.*"
def construct_select(custom_select = nil)
selected = custom_select || @reflection.options[:select] || "#{@reflection.table_name}.*"
end
def construct_joins
def construct_joins(custom_joins = nil)
if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to
reflection_primary_key = @reflection.klass.primary_key
source_primary_key = @reflection.source_reflection.primary_key_name
......@@ -96,8 +96,8 @@ def construct_joins
reflection_primary_key = @reflection.source_reflection.primary_key_name
source_primary_key = @reflection.klass.primary_key
end
"INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]}" % [
"INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]} #{custom_joins}" % [
@reflection.through_reflection.table_name,
@reflection.table_name, reflection_primary_key,
@reflection.through_reflection.table_name, source_primary_key
......
......@@ -63,6 +63,17 @@ def test_polymorphic_has_many_going_through_join_model_with_include_on_source_re
end
end
def test_polymorphic_has_many_going_through_join_model_with_disabled_include
assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first
assert_queries 1 do
tag.tagging
end
end
def test_polymorphic_has_many_going_through_join_model_with_custom_select_and_joins
assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first
tag.author_id
end
def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key
assert_equal tags(:misc), taggings(:welcome_general).super_tag
......
......@@ -21,7 +21,13 @@ def find_most_recent
has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id'
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings, :include => :tagging
has_many :tags, :through => :taggings, :include => :tagging do
def add_joins_and_select
find :all, :select => 'tags.*, authors.id as author_id', :include => false,
:joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id'
end
end
has_many :funky_tags, :through => :taggings, :class_name => 'Tag'
has_many :super_tags, :through => :taggings
has_one :tagging, :as => :taggable
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册