# How to clear ActiveRecord query cache on associations with dynamic table

**URL:** <https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708>\
**Category:** Rails\
**Created:** [February 18, 2013, 10:29pm UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708 "2013-02-18T22:29:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [February 18, 2013, 10:29pm UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708/1 "2013-02-18T22:29:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [February 18, 2013, 10:34pm UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708/2 "2013-02-18T22:34:04Z")

</div>

Model.uncached do  
Model.do.work  
end

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [February 18, 2013, 11:16pm UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708/3 "2013-02-18T23:16:44Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [February 27, 2013, 12:56am UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708/4 "2013-02-27T00:56:47Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [February 27, 2013, 11:20am UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708/5 "2013-02-27T11:20:44Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [July 23, 2013, 5:51pm UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708/6 "2013-07-23T17:51:43Z")

</div>

javinto wrote in post #1097693:

> 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:

It seems I have the exact same problem. Have you found a solution?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [July 23, 2013, 6:37pm UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708/7 "2013-07-23T18:37:49Z")

</div>

Unfortunately not. We redesigned the whole concept. Certainly not ideal,  
but that was the only way around.

I did not try it out in Rails 4 though which might be worth a try.

Good luck!

Op 23 jul. 2013, om 17:51 heeft Arnaud M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) het  
volgende geschreven:

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [July 23, 2013, 6:58pm UTC](https://www.ruby-forum.com/t/how-to-clear-activerecord-query-cache-on-associations-with-dynamic-table/226708/8 "2013-07-23T18:58:43Z")

</div>

Jan Verhoek wrote in post #1116383:

> Unfortunately not. We redesigned the whole concept. Certainly not ideal,  
> but that was the only way around.
> 
> I did not try it out in Rails 4 though which might be worth a try.
> 
> Good luck!
> 
> Op 23 jul. 2013, om 17:51 heeft Arnaud M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) het  
> volgende geschreven:

Ok, thanks for your answer. I’ll try to work around, definitely need  
some luck on this one!
