This project is archived and is in readonly mode.

#1242 ✓stale
John Wulff

AssociationReflection memoizes table_name and quoted_table_name

Reported by John Wulff | October 21st, 2008 @ 12:57 AM | in 3.x

Memoization in AssociationReflection causes problems when dynamically setting a model's table name. (This does not appear to be a problem in test and development environments due to reloading.)

We have partitioned one of our tables for performance reasons. Most of our data is in a partition that only admins have access too. The public only has access to a small partition of the table. We set the table name of our primary model, Node, depending on whether or not the user is an admin. We do this with the following around filter.


class Node < ActiveRecord::Base
  def self.scope_nodes_partition
    set_table_name 'nodes_publicly_viewable' unless current_user && current_user.is_admin?
    yield
  ensure
    set_table_name 'nodes'
  end
end

class ApplicationController < ActionController::Base
  around_filter :scope_nodes_partition
  def scope_nodes_partition
    Node.scope_nodes_partition do
      yield
    end
  end
end

This works fine for associations emanating from Node but any associations referencing Node memoize whichever table name Node had at the time the reflection is first used.


class Widget < ActiveRecord::Base
  belongs_to :node
end

Widget.reflections[:node].table_name => "nodes"
Node.set_table_name = 'nodes_publicly_viewable'
Widget.reflections[:node].table_name => "nodes"

The memoization "culprit" is in activerecord/lib/active_record/reflection.rb:


class AssociationReflection < MacroReflection
...
  def table_name
    @table_name ||= klass.table_name
  end

  def quoted_table_name
    @quoted_table_name ||= klass.quoted_table_name
  end
...

Removing the memoization like so:


class AssociationReflection < MacroReflection
...
  def table_name
    klass.table_name
  end

  def quoted_table_name
    klass.quoted_table_name
  end
...

... and the problem is fixed:


Widget.reflections[:node].table_name => "nodes"
Node.set_table_name = 'nodes_publicly_viewable'
Widget.reflections[:node].table_name => "nodes_publicly_viewable"

I understand that some might find dynamically changing a model's table name to be bad practice. But, in situations where there is no other simple solution it is very convenient. Additionally, I do not see how memoizing in this situation creates any efficiency.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages