Reputation: 10496
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
Upvotes: 0
Views: 1308
Reputation: 1416
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
Upvotes: 2