0

So with logic from this question: Instance variables through methods

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:

def index
    @posts = Post.all
end

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?

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)?

EDIT 1. @Sparda When i say "method-scope" i mean on following: I have controller named Demo and 2 actions inside, named action_1 and action_2.

Controller Demo
    def action_1
        @posts_1 = Post.all
    end

    def action_2
        @posts_2 = Post.where(#some_condition)
    end
 end

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 scenario 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.

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.

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.

I'm new to all of this, so don't yell :D

3
  • 2
    1.True 2.True 3.Expand... Commented Oct 22, 2013 at 19:10
  • 2
    For your question one, instance variable @posts is only available to all the methods in the controller during that request. Commented Oct 22, 2013 at 19:19
  • 2
    Regarding the last statement, it is true in a sense that Rails will typically instantiate a new controller and then call only one action on it based on the requested route, but it's not correct to refer to it as "method scope" since it isn't, local vars have method scope. Commented Oct 22, 2013 at 19:30

1 Answer 1

2

First of all, yes, instance variables can be changed in different methods of object, but ruby propagate you to use setters and getters in your class. So you actually never set it manually.

class Post 
  attr_accessor :message
end

is equal to

class Post

  def initialize
    @message
  end

  def message
    @message
  end

  def message=(m)
    @message = m
  end
end

so when you do

p = Post.new
p.message = "Hello" #=> that will call "message=" method
p.message           #=> that will call "message" method

Second to be said, Rails instance variables behaved exactly same way. You oftenly use them in models.

And last thing. You actually never call controller yourself, Rails do that for you. If you look at routes.rb file, you can find some routes like that:

get "posts", to: "posts#index"

which will do PostsController.action(:index). So whenever you call "/posts" you will get new instance of PostsController

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.