Note that there are some explanatory texts on larger screens.

plurals
  1. POInstance variables through methods in Ruby on rails
    primarykey
    data
    text
    <p>So with logic from this question: <a href="https://stackoverflow.com/questions/19525491/instance-variables-through-methods">Instance variables through methods</a></p> <p>Ruby on rails is framework built on Ruby so i assume that situation from above question propagate to rails also? So if i have controller Posts and method like:</p> <pre><code>def index @posts = Post.all end </code></pre> <p>Instance variable @posts is available to all actions in controller that have been called after index action? But if im getting this right, per each request to the server new instance of the controller is instantiated again and the old one is garbage-collected so i dont need to watch on this "cyclical" overwriting of instance variables? </p> <p>So in that case if we are not talking about sessions i can say that instance variables in rails have "method-function scope"(which technical is not right to say, because it isn't true, but seems like that)?</p> <p><strong>EDIT 1</strong>. @Sparda When i say "method-scope" i mean on following: I have controller named Demo and 2 actions inside, named <strong>action_1</strong> and <strong>action_2</strong>.<br></p> <pre><code>Controller Demo def action_1 @posts_1 = Post.all end def action_2 @posts_2 = Post.where(#some_condition) end end </code></pre> <p>So for first request to demo/action_1, controller is instantiated then action_1 is invoked and @posts_1 get created, then by default view get rendered and request is done. So instance of controller(and all instance variables) is garbage collected. In this <strong>scenario</strong> action_2 is never called so @posts_2 never created.(so of course @posts_2 is not available in action_1). Same happens for demo/action_2, @posts_1 never created.</p> <p>Now for demo/action_1, inside action_1 i want to call action_2 then @posts_1 get created and then @posts_2 get created, and now @posts_1 is available in action_2? If that is correct, if i named this variables(@posts_1 and @posts_2) with same name then this from action_2 will overwrite variable from action_1 of course.</p> <p><strong>So when i say "method-scope", which again i said it is NOT true i mean of this nature of frequent creation and destroy of controller object per request in that way i cant use instance variables from some other action(action_2, action_3, ..., action_N) if i'm going to demo/action_1.</strong></p> <p>I'm new to all of this, so don't yell :D</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload