We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/josh/rails.git
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
technoweenie (author)
Tue Mar 21 08:33:22 -0800 2006
commit  6fbf40823851c9f99c86c1287af0cfca725776e0
tree    1e1c31909175fdbc6c1e13bd0ab86b21dcb18592
parent  c8470f8a5b74eefd498397a8effdf37b4dc28674
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
0
@@ -1,5 +1,19 @@
0
 *SVN*
0
 
0
+* Allow overriding of find parameters in scoped has_many :through calls [Rick Olson]
0
+
0
+ In this example, :include => false disables the default eager association from loading. :select changes the standard
0
+ select clause. :joins specifies a join that is added to the end of the has_many :through query.
0
+
0
+ class Post < ActiveRecord::Base
0
+ has_many :tags, :through => :taggings, :include => :tagging do
0
+ def add_joins_and_select
0
+ find :all, :select => 'tags.*, authors.id as author_id', :include => false,
0
+ :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id'
0
+ end
0
+ end
0
+ end
0
+
0
 * 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]
0
 
0
 * Don't classify the has_one class when eager loading, it is already singular. Add tests. (closes #4117) [jonathan@bluewire.net.nz]
...
24
25
26
27
28
29
30
 
 
 
 
31
32
33
...
84
85
86
87
88
 
 
89
90
91
 
92
93
94
...
96
97
98
99
100
 
 
101
102
103
...
24
25
26
 
 
 
 
27
28
29
30
31
32
33
...
84
85
86
 
 
87
88
89
90
 
91
92
93
94
...
96
97
98
 
 
99
100
101
102
103
0
@@ -24,10 +24,10 @@ module ActiveRecord
0
           options[:order] = @reflection.options[:order]
0
         end
0
         
0
- options[:select] = construct_select
0
- options[:from] = construct_from
0
- options[:joins] = construct_joins
0
- options[:include] ||= @reflection.source_reflection.options[:include]
0
+ options[:select] = construct_select(options[:select])
0
+ options[:from] ||= construct_from
0
+ options[:joins] = construct_joins(options[:joins])
0
+ options[:include] = @reflection.source_reflection.options[:include] if options[:include].nil?
0
         
0
         merge_options_from_reflection!(options)
0
 
0
@@ -84,11 +84,11 @@ module ActiveRecord
0
           @reflection.table_name
0
         end
0
         
0
- def construct_select
0
- selected = @reflection.options[:select] || "#{@reflection.table_name}.*"
0
+ def construct_select(custom_select = nil)
0
+ selected = custom_select || @reflection.options[:select] || "#{@reflection.table_name}.*"
0
         end
0
         
0
- def construct_joins
0
+ def construct_joins(custom_joins = nil)
0
           if @reflection.through_reflection.options[:as] || @reflection.source_reflection.macro == :belongs_to
0
             reflection_primary_key = @reflection.klass.primary_key
0
             source_primary_key = @reflection.source_reflection.primary_key_name
0
@@ -96,8 +96,8 @@ module ActiveRecord
0
             reflection_primary_key = @reflection.source_reflection.primary_key_name
0
             source_primary_key = @reflection.klass.primary_key
0
           end
0
-
0
- "INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]}" % [
0
+
0
+ "INNER JOIN %s ON %s.%s = %s.%s #{@reflection.options[:joins]} #{custom_joins}" % [
0
             @reflection.through_reflection.table_name,
0
             @reflection.table_name, reflection_primary_key,
0
             @reflection.through_reflection.table_name, source_primary_key
...
63
64
65
 
 
 
 
 
 
 
 
 
 
 
66
67
68
...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
0
@@ -63,6 +63,17 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
0
     end
0
   end
0
 
0
+ def test_polymorphic_has_many_going_through_join_model_with_disabled_include
0
+ assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first
0
+ assert_queries 1 do
0
+ tag.tagging
0
+ end
0
+ end
0
+
0
+ def test_polymorphic_has_many_going_through_join_model_with_custom_select_and_joins
0
+ assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first
0
+ tag.author_id
0
+ end
0
 
0
   def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key
0
     assert_equal tags(:misc), taggings(:welcome_general).super_tag
...
21
22
23
24
 
 
 
 
 
 
 
25
26
27
...
21
22
23
 
24
25
26
27
28
29
30
31
32
33
0
@@ -21,7 +21,13 @@ class Post < ActiveRecord::Base
0
   has_and_belongs_to_many :special_categories, :join_table => "categories_posts", :association_foreign_key => 'category_id'
0
 
0
   has_many :taggings, :as => :taggable
0
- has_many :tags, :through => :taggings, :include => :tagging
0
+ has_many :tags, :through => :taggings, :include => :tagging do
0
+ def add_joins_and_select
0
+ find :all, :select => 'tags.*, authors.id as author_id', :include => false,
0
+ :joins => 'left outer join posts on taggings.taggable_id = posts.id left outer join authors on posts.author_id = authors.id'
0
+ end
0
+ end
0
+
0
   has_many :funky_tags, :through => :taggings, :class_name => 'Tag'
0
   has_many :super_tags, :through => :taggings
0
   has_one :tagging, :as => :taggable

Comments

    No one has commented yet.