diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-06 11:29:40 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-06 11:29:40 -0700 |
commit | 59ba85fa5e31965130632d0c90c508693b14b086 (patch) | |
tree | 1cd5986da0779b216b11853774a6771f65536529 /activerecord/lib | |
parent | 97a19c6b4b71d1e12d62d880ec8c8eed357b3d3c (diff) | |
download | rails-59ba85fa5e31965130632d0c90c508693b14b086.tar.gz rails-59ba85fa5e31965130632d0c90c508693b14b086.tar.bz2 rails-59ba85fa5e31965130632d0c90c508693b14b086.zip |
auto link hm:t fixture join tables like habtm
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/fixtures.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index a36dc8cd1b..f185581dda 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -623,6 +623,10 @@ module ActiveRecord row[fk_name] = ActiveRecord::FixtureSet.identify(value) end + when :has_many + if association.options[:through] + handle_hmt(rows, row, association) + end when :has_and_belongs_to_many handle_habtm(rows, row, association) end @@ -639,6 +643,18 @@ module ActiveRecord @primary_key_name ||= model_class && model_class.primary_key end + def handle_hmt(rows, row, association) + # This is the case when the join table has no fixtures file + 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.through_reflection.foreign_key => row[primary_key_name], + association.foreign_key => ActiveRecord::FixtureSet.identify(target) } + } + end + end + def handle_habtm(rows, row, association) # This is the case when the join table has no fixtures file if (targets = row.delete(association.name.to_s)) |