How does ActiveRecord::Relation get added to Rails' models and why does each model have individual Relation class?why am i seeing User::ActiveRecord_Relation instead of ActiveRecord::Relation?How can an ActiveRecord::Relation object call class methodsHow do I get the current absolute URL in Ruby on Rails?How to return an empty ActiveRecord relation?How to remove a key from Hash and get the remaining hash in Ruby/Rails?How to prevent ActiveRecord to put the namespace of a model in the colum 'type' with PolymorphismHow does “empty?” work on Rails model objects?Rails4: Get an ActiveRecord::Relation from a model or an already chained relationWhy does my new class method in a Rails model result in nil?Using a non-ActiveRecord model, can't access it through rails consoleHow to return ActiveRecord Relation in GraphQL (Rails)?Get an activerecord relation from a model that is lazy loaded

Minimum value of 4 digit number divided by sum of its digits

Phrase for the opposite of "foolproof"

Will a top journal at least read my introduction?

In gnome-terminal only 2 out of 3 zoom keys work

How deep to place a deadman anchor for a slackline?

Why the difference in metal between 銀行 and お金?

A non-technological, repeating, visible object in the sky, holding its position in the sky for hours

What does "rf" mean in "rfkill"?

How to back up a running remote server?

How can I record the screen and the rear camera on an iPhone simultaneously?

Are Boeing 737-800’s grounded?

What is the difference between `a[bc]d` (brackets) and `ab,cd` (braces)?

What is a Recurrent Neural Network?

When and why did journal article titles become descriptive, rather than creatively allusive?

Is it possible to measure lightning discharges as Nikola Tesla?

gnu parallel how to use with ffmpeg

Were there two appearances of Stan Lee?

Subtleties of choosing the sequence of tenses in Russian

Counterexample: a pair of linearly ordered sets that are isomorphic to subsets of the other, but not isomorphic between them

Do I have to worry about players making “bad” choices on level up?

Can a creature tell when it has been affected by a Divination wizard's Portent?

Why is the origin of “threshold” uncertain?

Find the coordinate of two line segments that are perpendicular

Is it possible to Ready a spell to be cast just before the start of your next turn by having the trigger be an ally's attack?



How does ActiveRecord::Relation get added to Rails' models and why does each model have individual Relation class?


why am i seeing User::ActiveRecord_Relation instead of ActiveRecord::Relation?How can an ActiveRecord::Relation object call class methodsHow do I get the current absolute URL in Ruby on Rails?How to return an empty ActiveRecord relation?How to remove a key from Hash and get the remaining hash in Ruby/Rails?How to prevent ActiveRecord to put the namespace of a model in the colum 'type' with PolymorphismHow does “empty?” work on Rails model objects?Rails4: Get an ActiveRecord::Relation from a model or an already chained relationWhy does my new class method in a Rails model result in nil?Using a non-ActiveRecord model, can't access it through rails consoleHow to return ActiveRecord Relation in GraphQL (Rails)?Get an activerecord relation from a model that is lazy loaded






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Suppose I have a Rails model: class Project < ActiveRecord::Base



In the Rails console:



> Project.all
=> #<ActiveRecord::Relation []>


That seems reasonable. However,



> Project.all.class
=> Project::ActiveRecord_Relation


What is Project::ActiveRecord_Relation? Specifically,



  • How did it get "added" (namespaced into) to my model class?

  • How does it respond to is_a? correctly? Project.all.is_a?(ActiveRecord::Relation) returns true (which is expected), but is Project::ActiveRecord_Relation actually an instance of ActiveRecord::Relation, or is it something else?

  • Why do this? Why doesn't Project.all return an ActiveRecord::Relation, rather than Project::ActiveRecord_Relation?

(This is in the context of Rails 5.1, in case it's changed in older or newer versions.)



(I'm open to title edits if someone else can come up with a better title for this question)










share|improve this question






























    1















    Suppose I have a Rails model: class Project < ActiveRecord::Base



    In the Rails console:



    > Project.all
    => #<ActiveRecord::Relation []>


    That seems reasonable. However,



    > Project.all.class
    => Project::ActiveRecord_Relation


    What is Project::ActiveRecord_Relation? Specifically,



    • How did it get "added" (namespaced into) to my model class?

    • How does it respond to is_a? correctly? Project.all.is_a?(ActiveRecord::Relation) returns true (which is expected), but is Project::ActiveRecord_Relation actually an instance of ActiveRecord::Relation, or is it something else?

    • Why do this? Why doesn't Project.all return an ActiveRecord::Relation, rather than Project::ActiveRecord_Relation?

    (This is in the context of Rails 5.1, in case it's changed in older or newer versions.)



    (I'm open to title edits if someone else can come up with a better title for this question)










    share|improve this question


























      1












      1








      1








      Suppose I have a Rails model: class Project < ActiveRecord::Base



      In the Rails console:



      > Project.all
      => #<ActiveRecord::Relation []>


      That seems reasonable. However,



      > Project.all.class
      => Project::ActiveRecord_Relation


      What is Project::ActiveRecord_Relation? Specifically,



      • How did it get "added" (namespaced into) to my model class?

      • How does it respond to is_a? correctly? Project.all.is_a?(ActiveRecord::Relation) returns true (which is expected), but is Project::ActiveRecord_Relation actually an instance of ActiveRecord::Relation, or is it something else?

      • Why do this? Why doesn't Project.all return an ActiveRecord::Relation, rather than Project::ActiveRecord_Relation?

      (This is in the context of Rails 5.1, in case it's changed in older or newer versions.)



      (I'm open to title edits if someone else can come up with a better title for this question)










      share|improve this question
















      Suppose I have a Rails model: class Project < ActiveRecord::Base



      In the Rails console:



      > Project.all
      => #<ActiveRecord::Relation []>


      That seems reasonable. However,



      > Project.all.class
      => Project::ActiveRecord_Relation


      What is Project::ActiveRecord_Relation? Specifically,



      • How did it get "added" (namespaced into) to my model class?

      • How does it respond to is_a? correctly? Project.all.is_a?(ActiveRecord::Relation) returns true (which is expected), but is Project::ActiveRecord_Relation actually an instance of ActiveRecord::Relation, or is it something else?

      • Why do this? Why doesn't Project.all return an ActiveRecord::Relation, rather than Project::ActiveRecord_Relation?

      (This is in the context of Rails 5.1, in case it's changed in older or newer versions.)



      (I'm open to title edits if someone else can come up with a better title for this question)







      ruby-on-rails ruby activerecord






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 22 at 21:18









      chumakoff

      4,31911233




      4,31911233










      asked Mar 22 at 19:07









      FeifanZFeifanZ

      14.8k63875




      14.8k63875






















          2 Answers
          2






          active

          oldest

          votes


















          2














          There are actually two questions you are asking:



          1. How does it work?

          2. Why is it like that? (What for?)

          @arieljuod has already given you some explanations and a link.



          However the second question is still unanswered.



          There is another similar question exists which I hope will help you find all the answers:



          How can an ActiveRecord::Relation object call class methods



          It looks like the two questions (by the link and yours one) answer each other )



          Take a look at @nikita-shilnikov's answer. Good luck in your investigation!






          share|improve this answer

























          • Very helpful, thank you! I didn't realize it was possible to use class methods as scopes, so didn't think to go down that line of exploration. It seems like being able to support that is the primary motivation behind this pattern.

            – FeifanZ
            Mar 23 at 3:05


















          2














          Check this line of code from ActiveRecord.



          https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/relation/delegation.rb#L23



          mangled_name = klass.name.gsub("::", "_")


          So, for your questions:



          • it get's added on activerecord's base when it extendes the delegation module https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/base.rb#L290


          • it's actually the same class, just something like an alias (not actually an alias, it's a constant with the class as value)


          • the class is actually an ActiveRecord::Relation, it's just that the name was changed






          share|improve this answer


















          • 1





            And all the class methods from Project get mixed into Project::ActiveRecord_Relation, that's how you can say Project.where(...).some_scope and also why you can implement scopes as explicit class methods.

            – mu is too short
            Mar 22 at 21:29











          • Thanks! I didn't realize class methods were available as scopes; seems like that's the key insight I missed here. I upvoted this answer, but I'm going to give @chumakoff the ✅ because he better addressed the "what for"

            – FeifanZ
            Mar 23 at 3:06












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306333%2fhow-does-activerecordrelation-get-added-to-rails-models-and-why-does-each-mod%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          There are actually two questions you are asking:



          1. How does it work?

          2. Why is it like that? (What for?)

          @arieljuod has already given you some explanations and a link.



          However the second question is still unanswered.



          There is another similar question exists which I hope will help you find all the answers:



          How can an ActiveRecord::Relation object call class methods



          It looks like the two questions (by the link and yours one) answer each other )



          Take a look at @nikita-shilnikov's answer. Good luck in your investigation!






          share|improve this answer

























          • Very helpful, thank you! I didn't realize it was possible to use class methods as scopes, so didn't think to go down that line of exploration. It seems like being able to support that is the primary motivation behind this pattern.

            – FeifanZ
            Mar 23 at 3:05















          2














          There are actually two questions you are asking:



          1. How does it work?

          2. Why is it like that? (What for?)

          @arieljuod has already given you some explanations and a link.



          However the second question is still unanswered.



          There is another similar question exists which I hope will help you find all the answers:



          How can an ActiveRecord::Relation object call class methods



          It looks like the two questions (by the link and yours one) answer each other )



          Take a look at @nikita-shilnikov's answer. Good luck in your investigation!






          share|improve this answer

























          • Very helpful, thank you! I didn't realize it was possible to use class methods as scopes, so didn't think to go down that line of exploration. It seems like being able to support that is the primary motivation behind this pattern.

            – FeifanZ
            Mar 23 at 3:05













          2












          2








          2







          There are actually two questions you are asking:



          1. How does it work?

          2. Why is it like that? (What for?)

          @arieljuod has already given you some explanations and a link.



          However the second question is still unanswered.



          There is another similar question exists which I hope will help you find all the answers:



          How can an ActiveRecord::Relation object call class methods



          It looks like the two questions (by the link and yours one) answer each other )



          Take a look at @nikita-shilnikov's answer. Good luck in your investigation!






          share|improve this answer















          There are actually two questions you are asking:



          1. How does it work?

          2. Why is it like that? (What for?)

          @arieljuod has already given you some explanations and a link.



          However the second question is still unanswered.



          There is another similar question exists which I hope will help you find all the answers:



          How can an ActiveRecord::Relation object call class methods



          It looks like the two questions (by the link and yours one) answer each other )



          Take a look at @nikita-shilnikov's answer. Good luck in your investigation!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 9 at 10:42

























          answered Mar 22 at 21:12









          chumakoffchumakoff

          4,31911233




          4,31911233












          • Very helpful, thank you! I didn't realize it was possible to use class methods as scopes, so didn't think to go down that line of exploration. It seems like being able to support that is the primary motivation behind this pattern.

            – FeifanZ
            Mar 23 at 3:05

















          • Very helpful, thank you! I didn't realize it was possible to use class methods as scopes, so didn't think to go down that line of exploration. It seems like being able to support that is the primary motivation behind this pattern.

            – FeifanZ
            Mar 23 at 3:05
















          Very helpful, thank you! I didn't realize it was possible to use class methods as scopes, so didn't think to go down that line of exploration. It seems like being able to support that is the primary motivation behind this pattern.

          – FeifanZ
          Mar 23 at 3:05





          Very helpful, thank you! I didn't realize it was possible to use class methods as scopes, so didn't think to go down that line of exploration. It seems like being able to support that is the primary motivation behind this pattern.

          – FeifanZ
          Mar 23 at 3:05













          2














          Check this line of code from ActiveRecord.



          https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/relation/delegation.rb#L23



          mangled_name = klass.name.gsub("::", "_")


          So, for your questions:



          • it get's added on activerecord's base when it extendes the delegation module https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/base.rb#L290


          • it's actually the same class, just something like an alias (not actually an alias, it's a constant with the class as value)


          • the class is actually an ActiveRecord::Relation, it's just that the name was changed






          share|improve this answer


















          • 1





            And all the class methods from Project get mixed into Project::ActiveRecord_Relation, that's how you can say Project.where(...).some_scope and also why you can implement scopes as explicit class methods.

            – mu is too short
            Mar 22 at 21:29











          • Thanks! I didn't realize class methods were available as scopes; seems like that's the key insight I missed here. I upvoted this answer, but I'm going to give @chumakoff the ✅ because he better addressed the "what for"

            – FeifanZ
            Mar 23 at 3:06
















          2














          Check this line of code from ActiveRecord.



          https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/relation/delegation.rb#L23



          mangled_name = klass.name.gsub("::", "_")


          So, for your questions:



          • it get's added on activerecord's base when it extendes the delegation module https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/base.rb#L290


          • it's actually the same class, just something like an alias (not actually an alias, it's a constant with the class as value)


          • the class is actually an ActiveRecord::Relation, it's just that the name was changed






          share|improve this answer


















          • 1





            And all the class methods from Project get mixed into Project::ActiveRecord_Relation, that's how you can say Project.where(...).some_scope and also why you can implement scopes as explicit class methods.

            – mu is too short
            Mar 22 at 21:29











          • Thanks! I didn't realize class methods were available as scopes; seems like that's the key insight I missed here. I upvoted this answer, but I'm going to give @chumakoff the ✅ because he better addressed the "what for"

            – FeifanZ
            Mar 23 at 3:06














          2












          2








          2







          Check this line of code from ActiveRecord.



          https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/relation/delegation.rb#L23



          mangled_name = klass.name.gsub("::", "_")


          So, for your questions:



          • it get's added on activerecord's base when it extendes the delegation module https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/base.rb#L290


          • it's actually the same class, just something like an alias (not actually an alias, it's a constant with the class as value)


          • the class is actually an ActiveRecord::Relation, it's just that the name was changed






          share|improve this answer













          Check this line of code from ActiveRecord.



          https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/relation/delegation.rb#L23



          mangled_name = klass.name.gsub("::", "_")


          So, for your questions:



          • it get's added on activerecord's base when it extendes the delegation module https://github.com/rails/rails/blob/f40860800c231ecd1daef6cf6b5a8a8eda76478d/activerecord/lib/active_record/base.rb#L290


          • it's actually the same class, just something like an alias (not actually an alias, it's a constant with the class as value)


          • the class is actually an ActiveRecord::Relation, it's just that the name was changed







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 19:42









          arieljuodarieljuod

          8,13211222




          8,13211222







          • 1





            And all the class methods from Project get mixed into Project::ActiveRecord_Relation, that's how you can say Project.where(...).some_scope and also why you can implement scopes as explicit class methods.

            – mu is too short
            Mar 22 at 21:29











          • Thanks! I didn't realize class methods were available as scopes; seems like that's the key insight I missed here. I upvoted this answer, but I'm going to give @chumakoff the ✅ because he better addressed the "what for"

            – FeifanZ
            Mar 23 at 3:06













          • 1





            And all the class methods from Project get mixed into Project::ActiveRecord_Relation, that's how you can say Project.where(...).some_scope and also why you can implement scopes as explicit class methods.

            – mu is too short
            Mar 22 at 21:29











          • Thanks! I didn't realize class methods were available as scopes; seems like that's the key insight I missed here. I upvoted this answer, but I'm going to give @chumakoff the ✅ because he better addressed the "what for"

            – FeifanZ
            Mar 23 at 3:06








          1




          1





          And all the class methods from Project get mixed into Project::ActiveRecord_Relation, that's how you can say Project.where(...).some_scope and also why you can implement scopes as explicit class methods.

          – mu is too short
          Mar 22 at 21:29





          And all the class methods from Project get mixed into Project::ActiveRecord_Relation, that's how you can say Project.where(...).some_scope and also why you can implement scopes as explicit class methods.

          – mu is too short
          Mar 22 at 21:29













          Thanks! I didn't realize class methods were available as scopes; seems like that's the key insight I missed here. I upvoted this answer, but I'm going to give @chumakoff the ✅ because he better addressed the "what for"

          – FeifanZ
          Mar 23 at 3:06






          Thanks! I didn't realize class methods were available as scopes; seems like that's the key insight I missed here. I upvoted this answer, but I'm going to give @chumakoff the ✅ because he better addressed the "what for"

          – FeifanZ
          Mar 23 at 3:06


















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306333%2fhow-does-activerecordrelation-get-added-to-rails-models-and-why-does-each-mod%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript