This project is archived and is in readonly mode.
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
-
John Wulff October 21st, 2008 @ 01:04 AM
- no changes were found...
-
DHH November 16th, 2008 @ 03:18 PM
- 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.
-
Santiago Pastorino February 2nd, 2011 @ 05:04 PM
- 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.
-
Santiago Pastorino February 2nd, 2011 @ 05:04 PM
- State changed from open to stale
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>