Skip to content

Instantly share code, notes, and snippets.

@meltzerj
Created April 24, 2011 21:24
Show Gist options
  • Select an option

  • Save meltzerj/939891 to your computer and use it in GitHub Desktop.

Select an option

Save meltzerj/939891 to your computer and use it in GitHub Desktop.
def build_table_aliases(from)
# for the targets
{}.tap do |aliases|
from.map(&:to_s).sort.map(&:to_sym).each_with_index do |plural, t_index|
begin
table = plural._as_class.table_name
rescue NameError => e
raise PolymorphicError, "Could not find a valid class for #{plural.inspect} (tried #{plural.to_s._classify}). If it's namespaced, be sure to specify it as :\"module/#{plural}\" instead."
end
begin
plural._as_class.columns.map(&:name).each_with_index do |field, f_index|
aliases["#{table}.#{field}"] = "t#{t_index}_r#{f_index}"
end
rescue ActiveRecord::StatementInvalid => e
_logger_warn "Looks like your table doesn't exist for #{plural.to_s._classify}.\nError #{e}\nSkipping..."
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment