Forum: Ruby on Rails How to clear ActiveRecord query cache on associations with dynamic table

Posted by javinto (Guest)
on 2013-02-18 22:29
(Received via mailing list)
In part of my application I'm using dynamic tables. I'm aware of the 
single
thread conditions. I've tackled some caveats yet - will post a blog 
about
it soon - but there's one I need help with.

Consider 2 models that are associated: Order and OrderLine where Order 
has
many order_lines.

Now we set the table names for them:
Order.table_name='v1_orders'
OrderLine.table_name='v1_order_lines'

Query:
OrderLine.joins(:order).where(Order.table_name=>{:customer_id=>1}).all
Result: Select v1_order_lines.* FROM v1_order_lines INNER JOIN v1_orders 
ON
v1_orders.id=v1_order_lines.order_id WHERE v1_orders.customer_id=1

So far so good

Now we alter the table names:
Order.table_name='v2_orders'
OrderLine.table_name='v2_order_lines'

and requery:

Query:
OrderLine.joins(:order).where(Order.table_name=>{:customer_id=>1}).all
Result: Select v2_order_lines.* FROM v2_order_lines *INNER JOIN 
v1_orders
ON v1_orders.id*=v2_order_lines.order_id WHERE v2_orders.customer_id=1

Notice the INNER JOIN, it still uses the v1_ prefixes!!

I looks like if there has been some association caching. How can I get 
rid
of it?

I tried: ActiveRecord::Base.connection.schema_cache.clear! but without 
the
right effect.


Thanks
Posted by Jordon Bedwell (Guest)
on 2013-02-18 22:34
(Received via mailing list)
Model.uncached do
  Model.do.work
end
Posted by javinto (Guest)
on 2013-02-18 23:16
(Received via mailing list)
Unfortunately the Model.uncached block does not work.

I tried ActiveRecord::Base.connection.disable_query_cache!
and
ActiveRecord::Base.connection.clear_query_cache
without any luck.

So, it's not the query cache. It looks like some sort of association 
cache.
Anyone suggestions?


Op maandag 18 februari 2013 22:27:24 UTC+1 schreef javinto het volgende:
Posted by Justin S. (justin_s)
on 2013-02-27 00:56
We are doing something similarly crazy as well, you might try:

    ActiveSupport::Dependencies::Reference.clear!

I had to dig deep within the bowels of Active Support for that one :)
Posted by Jan Verhoek (Guest)
on 2013-02-27 11:20
(Received via mailing list)
Thanks! I might need that.

There was another caveat I ran into: Active Record subclasses. They do 
not derive their table name dynamically from their parent class


Op 27 feb 2013, om 00:56 heeft Justin S. het volgende geschreven:
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.