How many connections does rails open in a recursive function?How can I “pretty” format my JSON output in Ruby on Rails?ActiveRecord has_one relationship does not return in certain casesHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?How to redirect to a 404 in Rails?How to drop columns using Rails migrationError message in add record formIn Rails, how can I make ActiveRecord associations for two models that reference each other in different ways?How to use concerns in Rails 4Complex association topic in Rails / mongoid with inverse foreign key
Two films in a tank, only one comes out with a development error – why?
What is a clear way to write a bar that has an extra beat?
Why doesn't H₄O²⁺ exist?
What's the output of a record needle playing an out-of-speed record
Is it unprofessional to ask if a job posting on GlassDoor is real?
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
Can I make popcorn with any corn?
Arrow those variables!
I'm flying to France today and my passport expires in less than 2 months
How does one intimidate enemies without having the capacity for violence?
Approximately how much travel time was saved by the opening of the Suez Canal in 1869?
Does detail obscure or enhance action?
Theorems that impeded progress
Has there ever been an airliner design involving reducing generator load by installing solar panels?
Why can't I see bouncing of switch on oscilloscope screen?
Today is the Center
Alternative to sending password over mail?
A case of the sniffles
Why are electrically insulating heatsinks so rare? Is it just cost?
How to move a thin line with the black arrow in Illustrator?
Why is consensus so controversial in Britain?
What defenses are there against being summoned by the Gate spell?
When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?
Mortgage Pre-approval / Loan - Apply Alone or with Fiancée?
How many connections does rails open in a recursive function?
How can I “pretty” format my JSON output in Ruby on Rails?ActiveRecord has_one relationship does not return in certain casesHow can I rename a database column in a Ruby on Rails migration?How do I get the current absolute URL in Ruby on Rails?How to redirect to a 404 in Rails?How to drop columns using Rails migrationError message in add record formIn Rails, how can I make ActiveRecord associations for two models that reference each other in different ways?How to use concerns in Rails 4Complex association topic in Rails / mongoid with inverse foreign key
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a model that has a recursive link to itself, like a linked list. If I'm writing a function like the below (pardon my syntax, Ruby is not my language of choice) to follow the chain to the end. Assuming this is part of an Active Record model and next
is a foreign key to the next node, what happens behind the scenes for this function? How many separate connections to my MySQL db is Active Record opening, and how long do they stick around for?
module LinkedList
class Node < ActiveRecord::Base
has_one :value
has_one :next, foreign_key: 'id' class_name: 'Source::Incident'
def fetch_all_nodes(current_node=nil, all_nodes=nil)
current_node = current_node ? current_node : self
all_nodes = all_nodes ? all_nodes : [self]
if current_node.next
all_nodes = fetch_all_nodes(current_node.next, all_nodes << current_node)
all_nodes
end
end
end
ruby-on-rails
add a comment |
I have a model that has a recursive link to itself, like a linked list. If I'm writing a function like the below (pardon my syntax, Ruby is not my language of choice) to follow the chain to the end. Assuming this is part of an Active Record model and next
is a foreign key to the next node, what happens behind the scenes for this function? How many separate connections to my MySQL db is Active Record opening, and how long do they stick around for?
module LinkedList
class Node < ActiveRecord::Base
has_one :value
has_one :next, foreign_key: 'id' class_name: 'Source::Incident'
def fetch_all_nodes(current_node=nil, all_nodes=nil)
current_node = current_node ? current_node : self
all_nodes = all_nodes ? all_nodes : [self]
if current_node.next
all_nodes = fetch_all_nodes(current_node.next, all_nodes << current_node)
all_nodes
end
end
end
ruby-on-rails
Can you add your basic model definition as well?
– Garrett Motzner
Mar 21 at 23:06
Fleshed it out a bit - this is meant to be a simple hypothetical example so if more detail is needed please let me know!
– Sam Mullin
Mar 22 at 1:04
add a comment |
I have a model that has a recursive link to itself, like a linked list. If I'm writing a function like the below (pardon my syntax, Ruby is not my language of choice) to follow the chain to the end. Assuming this is part of an Active Record model and next
is a foreign key to the next node, what happens behind the scenes for this function? How many separate connections to my MySQL db is Active Record opening, and how long do they stick around for?
module LinkedList
class Node < ActiveRecord::Base
has_one :value
has_one :next, foreign_key: 'id' class_name: 'Source::Incident'
def fetch_all_nodes(current_node=nil, all_nodes=nil)
current_node = current_node ? current_node : self
all_nodes = all_nodes ? all_nodes : [self]
if current_node.next
all_nodes = fetch_all_nodes(current_node.next, all_nodes << current_node)
all_nodes
end
end
end
ruby-on-rails
I have a model that has a recursive link to itself, like a linked list. If I'm writing a function like the below (pardon my syntax, Ruby is not my language of choice) to follow the chain to the end. Assuming this is part of an Active Record model and next
is a foreign key to the next node, what happens behind the scenes for this function? How many separate connections to my MySQL db is Active Record opening, and how long do they stick around for?
module LinkedList
class Node < ActiveRecord::Base
has_one :value
has_one :next, foreign_key: 'id' class_name: 'Source::Incident'
def fetch_all_nodes(current_node=nil, all_nodes=nil)
current_node = current_node ? current_node : self
all_nodes = all_nodes ? all_nodes : [self]
if current_node.next
all_nodes = fetch_all_nodes(current_node.next, all_nodes << current_node)
all_nodes
end
end
end
ruby-on-rails
ruby-on-rails
edited Mar 22 at 1:01
Sam Mullin
asked Mar 21 at 22:47
Sam MullinSam Mullin
6115
6115
Can you add your basic model definition as well?
– Garrett Motzner
Mar 21 at 23:06
Fleshed it out a bit - this is meant to be a simple hypothetical example so if more detail is needed please let me know!
– Sam Mullin
Mar 22 at 1:04
add a comment |
Can you add your basic model definition as well?
– Garrett Motzner
Mar 21 at 23:06
Fleshed it out a bit - this is meant to be a simple hypothetical example so if more detail is needed please let me know!
– Sam Mullin
Mar 22 at 1:04
Can you add your basic model definition as well?
– Garrett Motzner
Mar 21 at 23:06
Can you add your basic model definition as well?
– Garrett Motzner
Mar 21 at 23:06
Fleshed it out a bit - this is meant to be a simple hypothetical example so if more detail is needed please let me know!
– Sam Mullin
Mar 22 at 1:04
Fleshed it out a bit - this is meant to be a simple hypothetical example so if more detail is needed please let me know!
– Sam Mullin
Mar 22 at 1:04
add a comment |
2 Answers
2
active
oldest
votes
As written as a recursive function, you would have one call SQL call per node. Next is called, gets the related nodes, and caches the value. (You can reload it if needed). As far as I can tell this implementation ends up with a single object per database record. If you ended up looking up an object that you already had stored, that would cause more objects, and more database lookups, but Rails has a query cache, so that can reduce such lookups.
You could have all_nodes
instead return a lazy enumerator, and then if you don't end up inspecting the entire tail, you'd only have one call per node visited, and you could handle potentially infinite (or even looping) lists.
add a comment |
ActiveRecord
uses ActiveRecord::ConnectionAdapters::ConnectionPool
to connect to your datastore which has a default pool of 5 connections. So, at most, each instance of Rails/ActiveRecord will use a max number of database connections as the configured connection pool size.
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%2f55290365%2fhow-many-connections-does-rails-open-in-a-recursive-function%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
As written as a recursive function, you would have one call SQL call per node. Next is called, gets the related nodes, and caches the value. (You can reload it if needed). As far as I can tell this implementation ends up with a single object per database record. If you ended up looking up an object that you already had stored, that would cause more objects, and more database lookups, but Rails has a query cache, so that can reduce such lookups.
You could have all_nodes
instead return a lazy enumerator, and then if you don't end up inspecting the entire tail, you'd only have one call per node visited, and you could handle potentially infinite (or even looping) lists.
add a comment |
As written as a recursive function, you would have one call SQL call per node. Next is called, gets the related nodes, and caches the value. (You can reload it if needed). As far as I can tell this implementation ends up with a single object per database record. If you ended up looking up an object that you already had stored, that would cause more objects, and more database lookups, but Rails has a query cache, so that can reduce such lookups.
You could have all_nodes
instead return a lazy enumerator, and then if you don't end up inspecting the entire tail, you'd only have one call per node visited, and you could handle potentially infinite (or even looping) lists.
add a comment |
As written as a recursive function, you would have one call SQL call per node. Next is called, gets the related nodes, and caches the value. (You can reload it if needed). As far as I can tell this implementation ends up with a single object per database record. If you ended up looking up an object that you already had stored, that would cause more objects, and more database lookups, but Rails has a query cache, so that can reduce such lookups.
You could have all_nodes
instead return a lazy enumerator, and then if you don't end up inspecting the entire tail, you'd only have one call per node visited, and you could handle potentially infinite (or even looping) lists.
As written as a recursive function, you would have one call SQL call per node. Next is called, gets the related nodes, and caches the value. (You can reload it if needed). As far as I can tell this implementation ends up with a single object per database record. If you ended up looking up an object that you already had stored, that would cause more objects, and more database lookups, but Rails has a query cache, so that can reduce such lookups.
You could have all_nodes
instead return a lazy enumerator, and then if you don't end up inspecting the entire tail, you'd only have one call per node visited, and you could handle potentially infinite (or even looping) lists.
answered Mar 22 at 2:08
Garrett MotznerGarrett Motzner
823315
823315
add a comment |
add a comment |
ActiveRecord
uses ActiveRecord::ConnectionAdapters::ConnectionPool
to connect to your datastore which has a default pool of 5 connections. So, at most, each instance of Rails/ActiveRecord will use a max number of database connections as the configured connection pool size.
add a comment |
ActiveRecord
uses ActiveRecord::ConnectionAdapters::ConnectionPool
to connect to your datastore which has a default pool of 5 connections. So, at most, each instance of Rails/ActiveRecord will use a max number of database connections as the configured connection pool size.
add a comment |
ActiveRecord
uses ActiveRecord::ConnectionAdapters::ConnectionPool
to connect to your datastore which has a default pool of 5 connections. So, at most, each instance of Rails/ActiveRecord will use a max number of database connections as the configured connection pool size.
ActiveRecord
uses ActiveRecord::ConnectionAdapters::ConnectionPool
to connect to your datastore which has a default pool of 5 connections. So, at most, each instance of Rails/ActiveRecord will use a max number of database connections as the configured connection pool size.
answered Mar 22 at 3:02
Scott BartellScott Bartell
2,11621735
2,11621735
add a comment |
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%2f55290365%2fhow-many-connections-does-rails-open-in-a-recursive-function%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
Can you add your basic model definition as well?
– Garrett Motzner
Mar 21 at 23:06
Fleshed it out a bit - this is meant to be a simple hypothetical example so if more detail is needed please let me know!
– Sam Mullin
Mar 22 at 1:04