Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Rename AssociationReflection#primary_key_name to foreign_key, since t…

…he options key which it relates to is :foreign_key
  • Loading branch information...
commit 12675988813e82ac30f7c0e0008c12c4cf5d8cdc 1 parent 3c40062
@jonleighton jonleighton authored
View
22 activerecord/lib/active_record/association_preload.rb
@@ -205,7 +205,7 @@ def preload_has_and_belongs_to_many_association(records, reflection, preload_opt
# FIXME: options[:select] is always nil in the tests. Do we really
# need it?
options[:select] || left[Arel.star],
- right[reflection.primary_key_name].as(
+ right[reflection.foreign_key].as(
Arel.sql('the_parent_record_id'))
]
@@ -220,7 +220,7 @@ def preload_has_and_belongs_to_many_association(records, reflection, preload_opt
all_associated_records = associated_records(ids) do |some_ids|
method = in_or_equal(some_ids)
- conditions = right[reflection.primary_key_name].send(*method)
+ conditions = right[reflection.foreign_key].send(*method)
conditions = custom_conditions.inject(conditions) do |ast, cond|
ast.and cond
end
@@ -241,7 +241,7 @@ def preload_has_one_association(records, reflection, preload_options={})
unless through_records.empty?
through_reflection = reflections[options[:through]]
- through_primary_key = through_reflection.primary_key_name
+ through_primary_key = through_reflection.foreign_key
source = reflection.source_reflection.name
through_records.first.class.preload_associations(through_records, source)
if through_reflection.macro == :belongs_to
@@ -255,7 +255,7 @@ def preload_has_one_association(records, reflection, preload_options={})
end
end
else
- set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
+ set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.foreign_key)
end
end
@@ -263,8 +263,8 @@ def preload_has_many_association(records, reflection, preload_options={})
return if records.first.send(reflection.name).loaded?
options = reflection.options
- primary_key_name = reflection.through_reflection_primary_key_name
- id_to_record_map, ids = construct_id_map(records, primary_key_name || reflection.options[:primary_key])
+ foreign_key = reflection.through_reflection_foreign_key
+ id_to_record_map, ids = construct_id_map(records, foreign_key || reflection.options[:primary_key])
records.each {|record| record.send(reflection.name).loaded}
if options[:through]
@@ -281,7 +281,7 @@ def preload_has_many_association(records, reflection, preload_options={})
else
set_association_collection_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options),
- reflection.primary_key_name)
+ reflection.foreign_key)
end
end
@@ -319,7 +319,7 @@ def preload_through_records(records, reflection, through_association)
def preload_belongs_to_association(records, reflection, preload_options={})
return if records.first.send("loaded_#{reflection.name}?")
options = reflection.options
- primary_key_name = reflection.primary_key_name
+ foreign_key = reflection.foreign_key
klasses_and_ids = {}
@@ -330,7 +330,7 @@ def preload_belongs_to_association(records, reflection, preload_options={})
# to their parent_records
records.each do |record|
if klass = record.send(polymorph_type)
- klass_id = record.send(primary_key_name)
+ klass_id = record.send(foreign_key)
if klass_id
id_map = klasses_and_ids[klass.constantize] ||= {}
(id_map[klass_id.to_s] ||= []) << record
@@ -339,7 +339,7 @@ def preload_belongs_to_association(records, reflection, preload_options={})
end
else
id_map = records.group_by do |record|
- key = record.send(primary_key_name)
+ key = record.send(foreign_key)
key && key.to_s
end
id_map.delete nil
@@ -369,7 +369,7 @@ def find_associated_records(ids, reflection, preload_options)
conditions = []
- key = reflection.primary_key_name
+ key = reflection.foreign_key
if interface = reflection.options[:as]
key = "#{interface}_id"
View
4 activerecord/lib/active_record/associations.rb
@@ -1670,7 +1670,7 @@ def #{method_name}
class_eval <<-eoruby, __FILE__, __LINE__ + 1
def #{method_name}
association = #{reflection.name}
- association.update_attribute(#{reflection.primary_key_name.inspect}, nil) if association
+ association.update_attribute(#{reflection.foreign_key.inspect}, nil) if association
end
eoruby
when :restrict
@@ -1782,7 +1782,7 @@ def create_has_and_belongs_to_many_reflection(association_id, options, &extensio
reflection = create_reflection(:has_and_belongs_to_many, association_id, options, self)
- if reflection.association_foreign_key == reflection.primary_key_name
+ if reflection.association_foreign_key == reflection.foreign_key
raise HasAndBelongsToManyAssociationForeignKeyNeeded.new(reflection)
end
View
14 activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -32,7 +32,7 @@ def updated?
def stale_target?
if @target && @target.persisted?
target_id = @target[@reflection.association_primary_key].to_s
- foreign_key = @owner[@reflection.primary_key_name].to_s
+ foreign_key = @owner[@reflection.foreign_key].to_s
target_id != foreign_key
else
@@ -57,12 +57,12 @@ def update_counters(record)
# Checks whether record is different to the current target, without loading it
def different_target?(record)
- record.nil? && @owner[@reflection.primary_key_name] ||
- record.id != @owner[@reflection.primary_key_name]
+ record.nil? && @owner[@reflection.foreign_key] ||
+ record.id != @owner[@reflection.foreign_key]
end
def replace_keys(record)
- @owner[@reflection.primary_key_name] = record && record[@reflection.association_primary_key]
+ @owner[@reflection.foreign_key] = record && record[@reflection.association_primary_key]
end
def find_target
@@ -82,14 +82,14 @@ def construct_find_scope
def construct_conditions
conditions = aliased_table[@reflection.association_primary_key].
- eq(@owner[@reflection.primary_key_name])
+ eq(@owner[@reflection.foreign_key])
conditions = conditions.and(Arel.sql(sql_conditions)) if sql_conditions
conditions
end
def foreign_key_present
- !@owner[@reflection.primary_key_name].nil?
+ !@owner[@reflection.foreign_key].nil?
end
# NOTE - for now, we're only supporting inverse setting from belongs_to back onto
@@ -103,7 +103,7 @@ def target_id
if @reflection.options[:primary_key]
@owner.send(@reflection.name).try(:id)
else
- @owner[@reflection.primary_key_name]
+ @owner[@reflection.foreign_key]
end
end
end
View
2  activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -5,7 +5,7 @@ class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
def stale_target?
if @target && @target.persisted?
target_id = @target.send(@reflection.association_primary_key).to_s
- foreign_key = @owner.send(@reflection.primary_key_name).to_s
+ foreign_key = @owner.send(@reflection.foreign_key).to_s
target_type = @target.class.base_class.name
foreign_type = @owner.send(@reflection.options[:foreign_type]).to_s
View
8 activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
@@ -193,11 +193,11 @@ def join_has_many_through_to(relation)
first_key = second_key = nil
if through_reflection.macro == :belongs_to
- jt_primary_key = through_reflection.primary_key_name
+ jt_primary_key = through_reflection.foreign_key
jt_foreign_key = through_reflection.association_primary_key
else
jt_primary_key = through_reflection.active_record_primary_key
- jt_foreign_key = through_reflection.primary_key_name
+ jt_foreign_key = through_reflection.foreign_key
if through_reflection.options[:as] # has_many :through against a polymorphic join
jt_conditions <<
@@ -231,7 +231,7 @@ def join_has_many_through_to(relation)
join_table[reflection.source_reflection.options[:foreign_type]].
eq(reflection.options[:source_type])
else
- second_key = source_reflection.primary_key_name
+ second_key = source_reflection.foreign_key
end
end
@@ -262,7 +262,7 @@ def join_has_many_polymorphic_to(relation)
end
def join_belongs_to_to(relation)
- foreign_key = options[:foreign_key] || reflection.primary_key_name
+ foreign_key = options[:foreign_key] || reflection.foreign_key
primary_key = options[:primary_key] || reflection.klass.primary_key
join_target_table(
View
4 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -40,7 +40,7 @@ def insert_record(record, force = true, validate = true)
attributes = columns.map do |column|
name = column.name
value = case name.to_s
- when @reflection.primary_key_name.to_s
+ when @reflection.foreign_key.to_s
@owner.id
when @reflection.association_foreign_key.to_s
record.id
@@ -64,7 +64,7 @@ def delete_records(records)
records.each { |record| @owner.connection.delete(interpolate_sql(sql, record)) }
else
relation = Arel::Table.new(@reflection.options[:join_table])
- stmt = relation.where(relation[@reflection.primary_key_name].eq(@owner.id).
+ stmt = relation.where(relation[@reflection.foreign_key].eq(@owner.id).
and(relation[@reflection.association_foreign_key].in(records.map { |x| x.id }.compact))
).compile_delete
@owner.connection.delete stmt.to_sql
View
4 activerecord/lib/active_record/associations/has_association.rb
@@ -14,9 +14,9 @@ def set_owner_attributes(record)
def construct_owner_attributes(reflection = @reflection)
attributes = {}
if reflection.macro == :belongs_to
- attributes[reflection.association_primary_key] = @owner.send(reflection.primary_key_name)
+ attributes[reflection.association_primary_key] = @owner.send(reflection.foreign_key)
else
- attributes[reflection.primary_key_name] = @owner.send(reflection.active_record_primary_key)
+ attributes[reflection.foreign_key] = @owner.send(reflection.active_record_primary_key)
if reflection.options[:as]
attributes["#{reflection.options[:as]}_type"] = @owner.class.base_class.name
View
2  activerecord/lib/active_record/associations/has_many_association.rb
@@ -58,7 +58,7 @@ def delete_records(records)
when :delete_all
@reflection.klass.delete(records.map { |r| r.id })
else
- updates = { @reflection.primary_key_name => nil }
+ updates = { @reflection.foreign_key => nil }
conditions = { @reflection.association_primary_key => records.map { |r| r.id } }
with_scope(@scope) do
View
2  activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -31,7 +31,7 @@ def size
protected
def target_reflection_has_associated_record?
- if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.primary_key_name].blank?
+ if @reflection.through_reflection.macro == :belongs_to && @owner[@reflection.through_reflection.foreign_key].blank?
false
else
true
View
6 activerecord/lib/active_record/associations/has_one_association.rb
@@ -38,11 +38,11 @@ def replace(obj, dont_save = false)
@target.destroy if @target.persisted?
@owner.clear_association_cache
when :nullify
- @target[@reflection.primary_key_name] = nil
+ @target[@reflection.foreign_key] = nil
@target.save if @owner.persisted? && @target.persisted?
end
else
- @target[@reflection.primary_key_name] = nil
+ @target[@reflection.foreign_key] = nil
@target.save if @owner.persisted? && @target.persisted?
end
end
@@ -96,7 +96,7 @@ def new_record(replace_existing)
if replace_existing
replace(record, true)
else
- record[@reflection.primary_key_name] = @owner.id if @owner.persisted?
+ record[@reflection.foreign_key] = @owner.id if @owner.persisted?
self.target = record
set_inverse_instance(record)
end
View
10 activerecord/lib/active_record/associations/through_association.rb
@@ -13,7 +13,7 @@ def scoped
def stale_target?
if @target && @reflection.through_reflection.macro == :belongs_to && defined?(@through_foreign_key)
previous_key = @through_foreign_key.to_s
- current_key = @owner.send(@reflection.through_reflection.primary_key_name).to_s
+ current_key = @owner.send(@reflection.through_reflection.foreign_key).to_s
previous_key != current_key
else
@@ -69,14 +69,14 @@ def construct_joins
if @reflection.source_reflection.macro == :belongs_to
reflection_primary_key = @reflection.source_reflection.options[:primary_key] ||
@reflection.klass.primary_key
- source_primary_key = @reflection.source_reflection.primary_key_name
+ source_primary_key = @reflection.source_reflection.foreign_key
if @reflection.options[:source_type]
column = @reflection.source_reflection.options[:foreign_type]
conditions <<
right[column].eq(@reflection.options[:source_type])
end
else
- reflection_primary_key = @reflection.source_reflection.primary_key_name
+ reflection_primary_key = @reflection.source_reflection.foreign_key
source_primary_key = @reflection.source_reflection.options[:primary_key] ||
@reflection.through_reflection.klass.primary_key
if @reflection.source_reflection.options[:as]
@@ -100,7 +100,7 @@ def construct_join_attributes(associate)
raise ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection.new(@owner, @reflection) if [:has_one, :has_many].include?(@reflection.source_reflection.macro)
join_attributes = {
- @reflection.source_reflection.primary_key_name =>
+ @reflection.source_reflection.foreign_key =>
associate.send(@reflection.source_reflection.association_primary_key)
}
@@ -159,7 +159,7 @@ def build_sti_condition
def update_stale_state
if @reflection.through_reflection.macro == :belongs_to
- @through_foreign_key = @owner.send(@reflection.through_reflection.primary_key_name)
+ @through_foreign_key = @owner.send(@reflection.through_reflection.foreign_key)
end
end
end
View
6 activerecord/lib/active_record/autosave_association.rb
@@ -343,8 +343,8 @@ def save_has_one_association(reflection)
association.destroy
else
key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
- if autosave != false && (new_record? || association.new_record? || association[reflection.primary_key_name] != key || autosave)
- association[reflection.primary_key_name] = key
+ if autosave != false && (new_record? || association.new_record? || association[reflection.foreign_key] != key || autosave)
+ association[reflection.foreign_key] = key
saved = association.save(:validate => !autosave)
raise ActiveRecord::Rollback if !saved && autosave
saved
@@ -367,7 +367,7 @@ def save_belongs_to_association(reflection)
if association.updated?
association_id = association.send(reflection.options[:primary_key] || :id)
- self[reflection.primary_key_name] = association_id
+ self[reflection.foreign_key] = association_id
end
saved if autosave
View
2  activerecord/lib/active_record/fixtures.rb
@@ -634,7 +634,7 @@ def insert_fixtures
targets.each do |target|
join_fixtures["#{label}_#{target}"] = Fixture.new(
- { association.primary_key_name => row[primary_key_name],
+ { association.foreign_key => row[primary_key_name],
association.association_foreign_key => Fixtures.identify(target) },
nil, @connection)
end
View
14 activerecord/lib/active_record/reflection.rb
@@ -196,8 +196,8 @@ def quoted_table_name
@quoted_table_name ||= klass.quoted_table_name
end
- def primary_key_name
- @primary_key_name ||= options[:foreign_key] || derive_primary_key_name
+ def foreign_key
@sobrinho
sobrinho added a note

Can you create a depecrated method to not break old gems/plugins?

@jonleighton Collaborator

Hiya,

Thanks for the comment. I've talked to Aaron and yes, we will implement a deprecated version.

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ @foreign_key ||= options[:foreign_key] || derive_foreign_key
end
def primary_key_column
@@ -251,7 +251,7 @@ def through_reflection
false
end
- def through_reflection_primary_key_name
+ def through_reflection_foreign_key
end
def source_reflection
@@ -310,7 +310,7 @@ def derive_class_name
class_name
end
- def derive_primary_key_name
+ def derive_foreign_key
if belongs_to?
"#{name}_id"
elsif options[:as]
@@ -392,11 +392,11 @@ def check_validity!
end
def through_reflection_primary_key
- through_reflection.belongs_to? ? through_reflection.klass.primary_key : through_reflection.primary_key_name
+ through_reflection.belongs_to? ? through_reflection.klass.primary_key : through_reflection.foreign_key
end
- def through_reflection_primary_key_name
- through_reflection.primary_key_name if through_reflection.belongs_to?
+ def through_reflection_foreign_key
+ through_reflection.foreign_key if through_reflection.belongs_to?
end
private
View
2  activerecord/lib/active_record/relation/calculations.rb
@@ -211,7 +211,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
group_attr = @group_values
association = @klass.reflect_on_association(group_attr.first.to_sym)
associated = group_attr.size == 1 && association && association.macro == :belongs_to # only count belongs_to associations
- group_fields = Array(associated ? association.primary_key_name : group_attr)
+ group_fields = Array(associated ? association.foreign_key : group_attr)
group_aliases = group_fields.map { |field| column_alias_for(field) }
group_columns = group_aliases.zip(group_fields).map { |aliaz,field|
[aliaz, column_for(field)]
View
8 activerecord/test/cases/reflection_test.rb
@@ -8,6 +8,8 @@
require 'models/pirate'
require 'models/price_estimate'
require 'models/tagging'
+require 'models/author'
+require 'models/post'
class ReflectionTest < ActiveRecord::TestCase
include ActiveRecord::Reflection
@@ -127,11 +129,11 @@ def test_has_one_reflection
def test_belongs_to_inferred_foreign_key_from_assoc_name
Company.belongs_to :foo
- assert_equal "foo_id", Company.reflect_on_association(:foo).primary_key_name
+ assert_equal "foo_id", Company.reflect_on_association(:foo).foreign_key
Company.belongs_to :bar, :class_name => "Xyzzy"
- assert_equal "bar_id", Company.reflect_on_association(:bar).primary_key_name
+ assert_equal "bar_id", Company.reflect_on_association(:bar).foreign_key
Company.belongs_to :baz, :class_name => "Xyzzy", :foreign_key => "xyzzy_id"
- assert_equal "xyzzy_id", Company.reflect_on_association(:baz).primary_key_name
+ assert_equal "xyzzy_id", Company.reflect_on_association(:baz).foreign_key
end
def test_association_reflection_in_modules
Please sign in to comment.
Something went wrong with that request. Please try again.