From fc4ccf5a09cd9f160dbd417779e6e9f0c8c51512 Mon Sep 17 00:00:00 2001 From: James Carson Date: Wed, 16 Sep 2015 10:29:49 -0400 Subject: [PATCH] use rails4 join table naming conventions --- lib/mini_record/auto_schema.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/mini_record/auto_schema.rb b/lib/mini_record/auto_schema.rb index 9a7bf15..b29535c 100644 --- a/lib/mini_record/auto_schema.rb +++ b/lib/mini_record/auto_schema.rb @@ -320,7 +320,14 @@ class << connection; attr_accessor :table_definition; end unless connection.resp table = if name = association.options[:join_table] name.to_s else - [table_name, association.name.to_s].sort.join("_") + association_table_name = association.name.to_s.classify.constantize.table_name + table_name_substrings = [table_name,association_table_name].collect { |string| string.split('_') } + common_substrings = Array.new + table_name_substrings.first.each_index { |i| table_name_substrings.first[i] == table_name_substrings.last[i] ? common_substrings.push(table_name_substrings.first[i]) : break } + common_prefix = common_substrings.join('_') + table_names = [table_name,association_table_name].sort + table_names.last.gsub!(/^#{common_prefix}_/,'') + table_names.join("_") end unless connection.tables.include?(table.to_s) foreign_key = association.options[:foreign_key] || association.foreign_key