Giter Club home page Giter Club logo

Comments (5)

lcowell avatar lcowell commented on May 27, 2024

I've updated to the following without any change in behaviour.

pg 0.15.1
rails 3.2.13

I also add that it doesn't seem to be a problem if you're calling 'where' through a scope.

from apartment.

lcowell avatar lcowell commented on May 27, 2024

If I issue the same commands without the use of a block, I get the results I expect. eg.

Apartment::Database.switch("company_3")
User.where(:id => 100)
Apartment::Database.switch("company_4")
User.where(:id => 100)

I'm so confused it hurts. Why would running these command by iterating over a list have different behaviour than executing the commands sequentially ?

from apartment.

bradrobertson avatar bradrobertson commented on May 27, 2024

weeeird... I've definitely never seen that. We have quite a bit of code like that to loop through all tenants and do stuff with them, never had a problem.

I'll see if I can replicate it sometime this week and get back to you.

from apartment.

bradrobertson avatar bradrobertson commented on May 27, 2024

ah ok, I've been able to reproduce your problem, but it's just a problem with using the console and the way ActiveRecord::Relation's work.

When you say User.where(id: 1) you're not actually querying the database. You're just specifying criteria for a query. The actual query gets made at the moment that you want the data (so in your looping instance, it wants the return value)

The problem is, it loops once, builds a relation, loops again, builds a relation, then when it returns those two relations (and hence actually queries the db), they're within the scope of the 2nd Apartment Tenant

If you were do do something like:

%w{company_1 company_2}.map {|n| Apartment::Database.switch(n); User.where(id: 1).first }

Then it would work properly. That's why your find_by_id works.

Just remember that where is lazy, so you only actually make the query once the data is needed.

from apartment.

lcowell avatar lcowell commented on May 27, 2024

Thanks for the explanation. That makes perfect sense.

from apartment.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.