I have a scope that returns Array instead of ActiveRecord Relation when it is called, but if I call methods within the scope, it returns ActiveRecord Relation .
scope :beta_user, -> { joins(:config).where("config_type = 'Model' AND opts #>> '{beta}' = 'true'") } # Calling scope directly Model.beta_user # => [Model1, Model2, Model3] Model.beta_user.class # => Array # Calling scope definition Model.joins(:config).where("config_type = 'Model' AND opts #>> '{beta}' = 'true'") => [Model1, Model2, Model3] Model.joins(:config).where("config_type = 'Model' AND opts #>> '{beta}' = 'true'").class => Model::ActiveRecord_Relation
So my question is what happens to inconsistent return types? After that, I cannot bind other areas (I can still bind them before that), and I cannot use other AR Relation methods such as #order and #pluck .
From what I see in the console, it looks like calling Model.beta_user.class is still executing the request, while Model.joins(:config).where("config_type = 'Model' AND opts #>> '{beta}' = 'true'") does not execute the request. I thought that the area should not be executed until it is needed to optimize the chains / queries.
source share