mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
extract habtm handling to a method
This commit is contained in:
parent
7db93131ed
commit
5753a8cb2a
1 changed files with 12 additions and 8 deletions
|
@ -592,14 +592,7 @@ module ActiveRecord
|
||||||
row[fk_name] = ActiveRecord::FixtureSet.identify(value)
|
row[fk_name] = ActiveRecord::FixtureSet.identify(value)
|
||||||
end
|
end
|
||||||
when :has_and_belongs_to_many
|
when :has_and_belongs_to_many
|
||||||
if (targets = row.delete(association.name.to_s))
|
handle_habtm(rows, row, association)
|
||||||
targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
|
|
||||||
table_name = association.join_table
|
|
||||||
rows[table_name].concat targets.map { |target|
|
|
||||||
{ association.foreign_key => row[primary_key_name],
|
|
||||||
association.association_foreign_key => ActiveRecord::FixtureSet.identify(target) }
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -614,6 +607,17 @@ module ActiveRecord
|
||||||
@primary_key_name ||= model_class && model_class.primary_key
|
@primary_key_name ||= model_class && model_class.primary_key
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_habtm(rows, row, association)
|
||||||
|
if (targets = row.delete(association.name.to_s))
|
||||||
|
targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
|
||||||
|
table_name = association.join_table
|
||||||
|
rows[table_name].concat targets.map { |target|
|
||||||
|
{ association.foreign_key => row[primary_key_name],
|
||||||
|
association.association_foreign_key => ActiveRecord::FixtureSet.identify(target) }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def has_primary_key_column?
|
def has_primary_key_column?
|
||||||
@has_primary_key_column ||= primary_key_name &&
|
@has_primary_key_column ||= primary_key_name &&
|
||||||
model_class.columns.any? { |c| c.name == primary_key_name }
|
model_class.columns.any? { |c| c.name == primary_key_name }
|
||||||
|
|
Loading…
Reference in a new issue