I have been doing some mentoring for the firehose project. Recently my student was a bit puzzled as to why she could access an instance variable in a view but not access them in a model after declaring them in a controller. Long story short is that rails doesn’t try to pass instance variables to models (for good reason) so it is not done.
To further explain and shed some light on what I think rails is doing behind the scenes, I decided to whip up this quick example of how one might go about shuffling instance variables around.
To be clear, I am not saying rails actually does things this way, but only that in ruby moving values from one object to another is very easy and so the tendency to do such things happens naturally when using the language. That said, what’s natural for a seasoned ruby programmer could seem downright insane for new developers or developers with limited ruby knowledge.
Please feel free to take the code that follows, put it in a file and run it to see it in action. Try adding instance variables to see that it works without additional implementation code.