aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorBrad Greenlee <brad@wesabe.com>2008-06-02 22:04:46 -0700
committerMichael Koziarski <michael@koziarski.com>2008-07-06 20:25:10 +0200
commitafa0c7f728a8896c9ee9d932033e08a4c99dfd50 (patch)
tree6fd8642f524facf4d1d2584c21ce7b8d30b9d424 /activerecord/lib/active_record
parent3351d2997017465047b2c3dc63dc31e2362368af (diff)
downloadrails-afa0c7f728a8896c9ee9d932033e08a4c99dfd50.tar.gz
rails-afa0c7f728a8896c9ee9d932033e08a4c99dfd50.tar.bz2
rails-afa0c7f728a8896c9ee9d932033e08a4c99dfd50.zip
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>
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb3
-rwxr-xr-xactiverecord/lib/active_record/associations/has_one_association.rb11
2 files changed, 12 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index c5e8207f14..856d872bce 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -759,6 +759,7 @@ module ActiveRecord
# * <tt>:foreign_key</tt> - Specify the foreign key used for the association. By default this is guessed to be the name
# of this class in lower-case and "_id" suffixed. So a Person class that makes a +has_one+ association will use "person_id"
# as the default <tt>:foreign_key</tt>.
+ # * <tt>:primary_key</tt> - Specify the method that returns the primary key used for the association. By default this is +id+.
# * <tt>:include</tt> - Specify second-order associations that should be eager loaded when this object is loaded.
# * <tt>:as</tt> - Specifies a polymorphic interface (See <tt>belongs_to</tt>).
# * <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
@@ -1366,7 +1367,7 @@ module ActiveRecord
def create_has_one_reflection(association_id, options)
options.assert_valid_keys(
- :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly, :validate
+ :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly, :validate, :primary_key
)
create_reflection(:has_one, association_id, options, self)
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 25a268e95c..fdc0fa52c9 100755
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -47,7 +47,16 @@ module ActiveRecord
return (obj.nil? ? nil : self)
end
end
-
+
+ protected
+ def owner_quoted_id
+ if @reflection.options[:primary_key]
+ quote_value(@owner.send(@reflection.options[:primary_key]))
+ else
+ @owner.quoted_id
+ end
+ end
+
private
def find_target
@reflection.klass.find(:first,