Forum: Ruby on Rails Table_name question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
15720660e93e088518df105b1367feee?d=identicon&s=25 Daniyaruby (Guest)
on 2009-04-13 22:31
(Received via mailing list)
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)
This topic is locked and can not be replied to.