We can observe that the scope in table1 and this scope searches for some external data and uses the relation with the other model internally (table2). Everything works fine in our local environments because the database and the tables exist, but, when I wanted to deploy the new functionality to our production servers, I was greeted by an error:

The reason

Before running any rake task, scopes are being evaluated, so, any dependency with another model or table that is not created before will result in an error.

The solution with lambda

In all your scopes, where you have a dependency with another model, you must evaluate them with lambda. In my case, it works after I added lambda { after the name of the scope and its respective separation comma: