javinto
2013-Feb-18 21:27 UTC
How to clear ActiveRecord query cache on associations with dynamic table
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/esE-9LGzDZgJ. For more options, visit https://groups.google.com/groups/opt_out.
Jordon Bedwell
2013-Feb-18 21:32 UTC
Re: How to clear ActiveRecord query cache on associations with dynamic table
Model.uncached do Model.do.work end On Mon, Feb 18, 2013 at 3:27 PM, javinto <jan.javinto-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/esE-9LGzDZgJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
javinto
2013-Feb-18 22:15 UTC
Re: How to clear ActiveRecord query cache on associations with dynamic table
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:> > 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 > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/06Po7MkFlm8J. For more options, visit https://groups.google.com/groups/opt_out.
Justin S.
2013-Feb-26 23:56 UTC
Re: How to clear ActiveRecord query cache on associations with dynamic table
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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Jan Verhoek
2013-Feb-27 10:19 UTC
Re: Re: How to clear ActiveRecord query cache on associations with dynamic table
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:> 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 via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Arnaud M.
2013-Jul-23 15:51 UTC
Re: How to clear ActiveRecord query cache on associations with dynamic table
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! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0c67962058dc57acff243abe7858e041%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Arnaud M.
2013-Jul-23 16:58 UTC
Re: How to clear ActiveRecord query cache on associations with dynamic table
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. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> het > volgende geschreven:Ok, thanks for your answer. I''ll try to work around, definitely need some luck on this one! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a375a4b58dc29deb90d866324fc6610e%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.