public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added :select option to has_one and belongs_to, remove unused :order option on 
belongs_to.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#241 state:resolved]
josevalim (author)
Fri May 23 02:20:13 -0700 2008
NZKoz (committer)
Fri May 23 22:58:03 -0700 2008
commit  8d0b4fa39fab6e5d1e3382b4b137cc47e559b0be
tree    06a4ff2e8bbb4646082fd627ed6633d79a4ee949
parent  5b8d0f134a5766aa1b3d0945b79f04d2bb911c02
...
667
668
669
670
 
671
672
673
...
747
748
749
 
 
750
751
752
...
821
822
823
824
825
 
 
826
827
828
...
1011
1012
1013
1014
 
1015
1016
1017
...
1341
1342
1343
1344
 
1345
1346
1347
...
1349
1350
1351
1352
 
1353
1354
1355
1356
1357
1358
1359
 
1360
1361
1362
...
667
668
669
 
670
671
672
673
...
747
748
749
750
751
752
753
754
...
823
824
825
 
 
826
827
828
829
830
...
1013
1014
1015
 
1016
1017
1018
1019
...
1343
1344
1345
 
1346
1347
1348
1349
...
1351
1352
1353
 
1354
1355
1356
1357
1358
1359
1360
 
1361
1362
1363
1364
0
@@ -667,7 +667,7 @@ module ActiveRecord
0
       # * <tt>:limit</tt> - An integer determining the limit on the number of rows that should be returned.
0
       # * <tt>:offset</tt> - An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
0
       # * <tt>:select</tt> - By default, this is <tt>*</tt> as in <tt>SELECT * FROM</tt>, but can be changed if you, for example, want to do a join
0
-      #   but not include the joined columns.
0
+      #   but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will rise an error.
0
       # * <tt>:as</tt> - Specifies a polymorphic interface (See <tt>belongs_to</tt>).
0
       # * <tt>:through</tt> - Specifies a Join Model through which to perform the query.  Options for <tt>:class_name</tt> and <tt>:foreign_key</tt>
0
       #   are ignored, as the association uses the source reflection. You can only use a <tt>:through</tt> query through a <tt>belongs_to</tt>
0
@@ -747,6 +747,8 @@ module ActiveRecord
0
       #   as the default <tt>:foreign_key</tt>.
0
       # * <tt>:include</tt> - Specify second-order associations that should be eager loaded when this object is loaded.
0
       # * <tt>:as</tt> - Specifies a polymorphic interface (See <tt>belongs_to</tt>).
0
+      # * <tt>:select</tt> - By default, this is <tt>*</tt> as in <tt>SELECT * FROM</tt>, but can be changed if, for example, you want to do a join
0
+      #   but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will raise an error.
0
       # * <tt>:through</tt>: Specifies a Join Model through which to perform the query.  Options for <tt>:class_name</tt> and <tt>:foreign_key</tt>
0
       #   are ignored, as the association uses the source reflection. You can only use a <tt>:through</tt> query through a 
0
       #   <tt>has_one</tt> or <tt>belongs_to</tt> association on the join model.
0
@@ -821,8 +823,8 @@ module ActiveRecord
0
       #   if the real class name is Person, you'll have to specify it with this option.
0
       # * <tt>:conditions</tt> - Specify the conditions that the associated object must meet in order to be included as a +WHERE+
0
       #   SQL fragment, such as <tt>authorized = 1</tt>.
0
-      # * <tt>:order</tt> - Specify the order in which the associated objects are returned as an <tt>ORDER BY</tt> SQL fragment,
0
-      #   such as <tt>last_name, first_name DESC</tt>.
0
+      # * <tt>:select</tt> - By default, this is <tt>*</tt> as in <tt>SELECT * FROM</tt>, but can be changed if, for example, you want to do a join
0
+      #   but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will raise an error.
0
       # * <tt>:foreign_key</tt> - Specify the foreign key used for the association. By default this is guessed to be the name
0
       #   of the association with an "_id" suffix. So a class that defines a <tt>belongs_to :person</tt> association will use
0
       #   "person_id" as the default <tt>:foreign_key</tt>. Similarly, <tt>belongs_to :favorite_person, :class_name => "Person"</tt>
0
@@ -1011,7 +1013,7 @@ module ActiveRecord
0
       # * <tt>:limit</tt> - An integer determining the limit on the number of rows that should be returned.
0
       # * <tt>:offset</tt> - An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
0
       # * <tt>:select</tt> - By default, this is <tt>*</tt> as in <tt>SELECT * FROM</tt>, but can be changed if, for example, you want to do a join
0
-      #   but not include the joined columns.
0
+      #   but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will raise an error.
0
       # * <tt>:readonly</tt> - If true, all the associated objects are readonly through the association.
0
       #
0
       # Option examples:
0
@@ -1341,7 +1343,7 @@ module ActiveRecord
0
 
0
         def create_has_one_reflection(association_id, options)
0
           options.assert_valid_keys(
0
-            :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly
0
+            :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly
0
           )
0
 
0
           create_reflection(:has_one, association_id, options, self)
0
@@ -1349,14 +1351,14 @@ module ActiveRecord
0
         
0
         def create_has_one_through_reflection(association_id, options)
0
           options.assert_valid_keys(
0
-            :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source, :source_type
0
+            :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source, :source_type
0
           )
0
           create_reflection(:has_one, association_id, options, self)
0
         end
0
 
0
         def create_belongs_to_reflection(association_id, options)
0
           options.assert_valid_keys(
0
-            :class_name, :foreign_key, :foreign_type, :remote, :conditions, :order, :include, :dependent,
0
+            :class_name, :foreign_key, :foreign_type, :remote, :select, :conditions, :include, :dependent,
0
             :counter_cache, :extend, :polymorphic, :readonly
0
           )
0
 
...
42
43
44
45
 
 
46
47
48
 
49
50
51
...
42
43
44
 
45
46
47
48
 
49
50
51
52
0
@@ -42,10 +42,11 @@ module ActiveRecord
0
       private
0
         def find_target
0
           @reflection.klass.find(
0
-            @owner[@reflection.primary_key_name], 
0
+            @owner[@reflection.primary_key_name],
0
+            :select     => @reflection.options[:select],
0
             :conditions => conditions,
0
             :include    => @reflection.options[:include],
0
-            :readonly => @reflection.options[:readonly]
0
+            :readonly   => @reflection.options[:readonly]
0
           )
0
         end
0
 
...
29
30
31
32
 
 
33
34
35
36
37
 
38
39
40
...
29
30
31
 
32
33
34
35
36
37
 
38
39
40
41
0
@@ -29,12 +29,13 @@ module ActiveRecord
0
 
0
           if @reflection.options[:conditions]
0
             association_class.find(
0
-              @owner[@reflection.primary_key_name], 
0
+              @owner[@reflection.primary_key_name],
0
+              :select     => @reflection.options[:select],
0
               :conditions => conditions,
0
               :include    => @reflection.options[:include]
0
             )
0
           else
0
-            association_class.find(@owner[@reflection.primary_key_name], :include => @reflection.options[:include])
0
+            association_class.find(@owner[@reflection.primary_key_name], :select => @reflection.options[:select], :include => @reflection.options[:include])
0
           end
0
         end
0
 
...
51
52
53
54
 
 
55
56
57
 
58
59
60
...
51
52
53
 
54
55
56
57
 
58
59
60
61
0
@@ -51,10 +51,11 @@ module ActiveRecord
0
       private
0
         def find_target
0
           @reflection.klass.find(:first, 
0
-            :conditions => @finder_sql, 
0
+            :conditions => @finder_sql,
0
+            :select     => @reflection.options[:select],
0
             :order      => @reflection.options[:order], 
0
             :include    => @reflection.options[:include],
0
-            :readonly    => @reflection.options[:readonly]
0
+            :readonly   => @reflection.options[:readonly]
0
           )
0
         end
0
 
...
92
93
94
 
 
 
 
 
95
96
97
...
92
93
94
95
96
97
98
99
100
101
102
0
@@ -92,6 +92,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
0
     assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
0
   end
0
 
0
+  def test_with_select
0
+    assert_equal Company.find(2).firm_with_select.attributes.size, 1
0
+    assert_equal Company.find(2, :include => :firm_with_select ).firm_with_select.attributes.size, 1
0
+  end
0
+
0
   def test_belongs_to_counter
0
     debate = Topic.create("title" => "debate")
0
     assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
...
24
25
26
 
 
 
 
 
27
28
29
...
24
25
26
27
28
29
30
31
32
33
34
0
@@ -24,6 +24,11 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
0
     assert_queries(0) { firms.each(&:account) }
0
   end
0
 
0
+  def test_with_select
0
+    assert_equal Firm.find(1).account_with_select.attributes.size, 2
0
+    assert_equal Firm.find(1, :include => :account_with_select).attributes.size, 2
0
+  end
0
+
0
   def test_can_marshal_has_one_association_with_nil_target
0
     firm = Firm.new
0
     assert_nothing_raised do
...
47
48
49
 
50
51
52
...
64
65
66
 
67
68
69
...
47
48
49
50
51
52
53
...
65
66
67
68
69
70
71
0
@@ -47,6 +47,7 @@ class Firm < Company
0
   has_many :readonly_clients, :class_name => 'Client', :readonly => true
0
 
0
   has_one :account, :foreign_key => "firm_id", :dependent => :destroy
0
+  has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id"
0
   has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
0
 end
0
 
0
@@ -64,6 +65,7 @@ end
0
 class Client < Company
0
   belongs_to :firm, :foreign_key => "client_of"
0
   belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
0
+  belongs_to :firm_with_select, :class_name => "Firm", :foreign_key => "firm_id", :select => "id"
0
   belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
0
   belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1]
0
   belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true

Comments