Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

AssociationReflection memoizes table_name and quoted_table_name

#1242

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.

Reported by John Wulff · October 21st, 2008 @ 12:57 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. John Wulff
  2. DHH
    DHH
    • State changed from new to incomplete
    • Tag changed from activerecord, associations, reflection to activerecord, associations, patch, reflection

    This was done as part of a commit to speed up eager loading: http://github.com/rails/rails/co.... So if you're going to yank it out, we'll have to have some benchmarks showing it's not a big deal first.

    November 16th, 2008 @ 03:18 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from incomplete to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 05:04 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:04 PM