Apartment Multi tenant gem find method issue

I am trying to upgrade my application from rails version from 4.2 to 5.2.8. Everything went well but one corner case I am getting error. I debug lot of things but I didn’t able to fix it properly. In the above image, I’ve switch tenant with some X value but while find the Customer model(single tenant) it uses some unknown Tenant value Y. I think it uses the Cache value. Please guide me if any one had any idea. I am extending the Active Record find method and then added some monkey patch on it.

Inside the find method, I am using the some cache statement. It cause the problem.

        key = primary_key
         s = cached_find_by_statement(key) { |params|
            where(key => params.bind).limit(1)
          }
          record = s.execute([id], connection).first
1 Like

I had been wondering why this would be happening. Makes sense now!

Still, I am not able to find the Rails 5.2.8 equivalent statement for the above execute method calling without cached_find_by_statement.

Here, does anybody have idea about to run this statement without using cache.

    statement = cached_find_by_statement(key) { |params|
      where(key => params.bind).limit(1)
    }
    record = statement.execute([id], connection).first

Maybe try simply:

record = where(primary_key => id).limit(1).first

or in a WORST case scenario you can try:

record = find_by_sql(send(:sanitize_sql_array, ["SELECT * FROM #{table_name} WHERE #{primary_key} = ? LIMIT 1", [id]])).first

with Where clause it is working as expected but no luck. Still there is some issue with apartment gem.

It is not even coming to the find method while cart page loading, without that it is able to find the cart with previous tenant_id and then it returns 404.