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;
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)
returnstrue
(which is expected), but isProject::ActiveRecord_Relation
actually an instance ofActiveRecord::Relation
, or is it something else? - Why do this? Why doesn't
Project.all
return anActiveRecord::Relation
, rather thanProject::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
add a comment |
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)
returnstrue
(which is expected), but isProject::ActiveRecord_Relation
actually an instance ofActiveRecord::Relation
, or is it something else? - Why do this? Why doesn't
Project.all
return anActiveRecord::Relation
, rather thanProject::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
add a comment |
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)
returnstrue
(which is expected), but isProject::ActiveRecord_Relation
actually an instance ofActiveRecord::Relation
, or is it something else? - Why do this? Why doesn't
Project.all
return anActiveRecord::Relation
, rather thanProject::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
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)
returnstrue
(which is expected), but isProject::ActiveRecord_Relation
actually an instance ofActiveRecord::Relation
, or is it something else? - Why do this? Why doesn't
Project.all
return anActiveRecord::Relation
, rather thanProject::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
ruby-on-rails ruby activerecord
edited Mar 22 at 21:18
chumakoff
4,31911233
4,31911233
asked Mar 22 at 19:07
FeifanZFeifanZ
14.8k63875
14.8k63875
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There are actually two questions you are asking:
- How does it work?
- 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!
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
add a comment |
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
1
And all the class methods fromProject
get mixed intoProject::ActiveRecord_Relation
, that's how you can sayProject.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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
There are actually two questions you are asking:
- How does it work?
- 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!
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
add a comment |
There are actually two questions you are asking:
- How does it work?
- 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!
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
add a comment |
There are actually two questions you are asking:
- How does it work?
- 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!
There are actually two questions you are asking:
- How does it work?
- 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!
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
add a comment |
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
add a comment |
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
1
And all the class methods fromProject
get mixed intoProject::ActiveRecord_Relation
, that's how you can sayProject.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
add a comment |
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
1
And all the class methods fromProject
get mixed intoProject::ActiveRecord_Relation
, that's how you can sayProject.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
add a comment |
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
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
answered Mar 22 at 19:42
arieljuodarieljuod
8,13211222
8,13211222
1
And all the class methods fromProject
get mixed intoProject::ActiveRecord_Relation
, that's how you can sayProject.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
add a comment |
1
And all the class methods fromProject
get mixed intoProject::ActiveRecord_Relation
, that's how you can sayProject.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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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