aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb14
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb2
-rw-r--r--activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb8
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_association.rb4
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb2
-rw-r--r--activerecord/lib/active_record/associations/has_many_through_association.rb2
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb6
-rw-r--r--activerecord/lib/active_record/associations/through_association.rb10
9 files changed, 26 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index e4758f4369..63eb38ab34 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -32,7 +32,7 @@ module ActiveRecord
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 @@ module ActiveRecord
# 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 @@ module ActiveRecord
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 @@ module ActiveRecord
if @reflection.options[:primary_key]
@owner.send(@reflection.name).try(:id)
else
- @owner[@reflection.primary_key_name]
+ @owner[@reflection.foreign_key]
end
end
end
diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
index 6293c4ca42..46adc048b8 100644
--- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -5,7 +5,7 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
index 02707dfae1..1e5149d80f 100644
--- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
+++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
@@ -193,11 +193,11 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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(
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index b1d454545f..5336b6cc28 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -40,7 +40,7 @@ module ActiveRecord
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 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/has_association.rb b/activerecord/lib/active_record/associations/has_association.rb
index 0ecdb696ea..190fed77c2 100644
--- a/activerecord/lib/active_record/associations/has_association.rb
+++ b/activerecord/lib/active_record/associations/has_association.rb
@@ -14,9 +14,9 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index a0e2e8b479..0d044b28e4 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -58,7 +58,7 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb
index e2b008034e..2348ee099c 100644
--- a/activerecord/lib/active_record/associations/has_many_through_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -31,7 +31,7 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 04cb16b909..12ccb3af8a 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -38,11 +38,11 @@ module ActiveRecord
@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 @@ module ActiveRecord
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
diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb
index 65ca4b1282..c78f5d969f 100644
--- a/activerecord/lib/active_record/associations/through_association.rb
+++ b/activerecord/lib/active_record/associations/through_association.rb
@@ -13,7 +13,7 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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 @@ module ActiveRecord
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