public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/JackDanger/rails.git
Add support for :primary_key option to has_one as well as has_many so that 
a key other than the default primary key can be used for the association

Signed-off-by: Michael Koziarski <michael@koziarski.com>
bgreenlee (author)
Mon Jun 02 22:04:46 -0700 2008
NZKoz (committer)
Sun Jul 06 11:25:10 -0700 2008
commit  afa0c7f728a8896c9ee9d932033e08a4c99dfd50
tree    6fd8642f524facf4d1d2584c21ce7b8d30b9d424
parent  3351d2997017465047b2c3dc63dc31e2362368af
...
759
760
761
 
762
763
764
...
1366
1367
1368
1369
 
1370
1371
1372
...
759
760
761
762
763
764
765
...
1367
1368
1369
 
1370
1371
1372
1373
0
@@ -759,6 +759,7 @@ module ActiveRecord
0
       # * <tt>:foreign_key</tt> - Specify the foreign key used for the association. By default this is guessed to be the name
0
       # of this class in lower-case and "_id" suffixed. So a Person class that makes a +has_one+ association will use "person_id"
0
       # as the default <tt>:foreign_key</tt>.
0
+ # * <tt>:primary_key</tt> - Specify the method that returns the primary key used for the association. By default this is +id+.
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
@@ -1366,7 +1367,7 @@ module ActiveRecord
0
 
0
         def create_has_one_reflection(association_id, options)
0
           options.assert_valid_keys(
0
- :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly, :validate
0
+ :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly, :validate, :primary_key
0
           )
0
 
0
           create_reflection(:has_one, association_id, options, self)
...
47
48
49
50
 
 
 
 
 
 
 
 
 
 
51
52
53
...
47
48
49
 
50
51
52
53
54
55
56
57
58
59
60
61
62
0
@@ -47,7 +47,16 @@ module ActiveRecord
0
           return (obj.nil? ? nil : self)
0
         end
0
       end
0
-
0
+
0
+ protected
0
+ def owner_quoted_id
0
+ if @reflection.options[:primary_key]
0
+ quote_value(@owner.send(@reflection.options[:primary_key]))
0
+ else
0
+ @owner.quoted_id
0
+ end
0
+ end
0
+
0
       private
0
         def find_target
0
           @reflection.klass.find(:first,
...
29
30
31
 
 
 
 
 
 
 
32
33
34
...
29
30
31
32
33
34
35
36
37
38
39
40
41
0
@@ -29,6 +29,13 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
0
     assert_equal Firm.find(1, :include => :account_with_select).account_with_select.attributes.size, 2
0
   end
0
 
0
+ def test_finding_using_primary_key
0
+ firm = companies(:first_firm)
0
+ assert_equal Account.find_by_firm_id(firm.id), firm.account
0
+ firm.firm_id = companies(:rails_core).id
0
+ assert_equal accounts(:rails_core_account), firm.account_using_primary_key
0
+ end
0
+
0
   def test_can_marshal_has_one_association_with_nil_target
0
     firm = Firm.new
0
     assert_nothing_raised do
...
160
161
162
163
164
165
 
 
 
166
167
168
...
160
161
162
 
 
 
163
164
165
166
167
168
0
@@ -160,9 +160,9 @@ class ReflectionTest < ActiveRecord::TestCase
0
 
0
   def test_reflection_of_all_associations
0
     # FIXME these assertions bust a lot
0
- assert_equal 22, Firm.reflect_on_all_associations.size
0
- assert_equal 17, Firm.reflect_on_all_associations(:has_many).size
0
- assert_equal 5, Firm.reflect_on_all_associations(:has_one).size
0
+ assert_equal 24, Firm.reflect_on_all_associations.size
0
+ assert_equal 18, Firm.reflect_on_all_associations(:has_many).size
0
+ assert_equal 6, Firm.reflect_on_all_associations(:has_one).size
0
     assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size
0
   end
0
 
...
53
54
55
 
56
57
58
...
53
54
55
56
57
58
59
0
@@ -53,6 +53,7 @@ class Firm < Company
0
   has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false
0
   has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id", :class_name=>'Account'
0
   has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true
0
+ has_one :account_using_primary_key, :primary_key => "firm_id", :class_name => "Account"
0
 end
0
 
0
 class DependentFirm < Company

Comments

    No one has commented yet.