Table_name question


#1

Hello all!

I want to dynamically change table name for some count of my models.
I understand that may be it’s bad practice but I simply need it.

I found some solution that works for somebody:

application.rb:
around_filter :scope_rubrics_partition

def scope_rubrics_partition
Rubric.scope_rubrics_partition do
yield
end
end

class Rubric < ActiveRecord::Base
def self.scope_rubrics_partition
if true
set_table_name ‘dp_ru_rubrics’
# set_primary_key ‘dp_ru_rubrics.id’
else
set_table_name ‘rubrics’
end
yield
ensure
set_table_name ‘rubrics’
end
end

Also patch rails to don’t memoize table_name
reflections.rb:
def table_name
klass.table_name
end

  def quoted_table_name
    klass.quoted_table_name
  end

But now I get an error like:

Mysql::Error: Unknown column ‘rubrics.id’ in ‘order clause’: SELECT
dp_ru_rubrics.* FROM dp_ru_rubrics ORDER BY rubrics.id ASC
LIMIT 0, 15

So, how in the end set table name dynamically in production mode too?
Thank you!
p.s: sorry for poor english)