Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The first thing to remember, before we even get into your questions, is that all variables, regardless of scope, are only active for the current request. You can use config values for persistence, but a global will expire at the end of a request, same as an instance variable.</p> <ol> <li><p>Instance variables that are set in the controller are available to the view. Models only have access to variables that have been directly passed to the Class or instance. This means that if you have a <code>@first_name</code> variable, in your controller or view, you won't be able to see it in your model. If you wanted to use it in your model, you would have to do something like <code>MyModelName.new( :first_name =&gt; @first_name )</code> or <code>an_instance_of_my_modelname.some_method_i_have_added( @first_name )</code>.</p></li> <li><p>They don't have to be methods, per se, but they almost certainly will end up being methods. Your most typical use of "variables" in a model would be the attributes. Attributes on a model are available to the instance of the model, regardless or whether it is in the view, controller, helper, or where ever. Attributes work by basically defining a setter and a getter method, behind the scenes (an attribute of <code>first_name</code> would make method <code>first_name()</code> and <code>first_name=()</code>). These methods can even be overridden in the model, to manipulate the values prior to insertion or removal from the database. You can achieve a similar effect without the database, using <a href="http://apidock.com/ruby/Module/attr_accessor" rel="noreferrer">http://apidock.com/ruby/Module/attr_accessor</a>. Class methods are the same in scope, but operate on the Class, rather than a specific instance.</p></li> <li><p>Sorta the same as the answers for 1 and 2... ;) Variables (but not constants) set in models must be exposed via a method to be available in the controllers and views. Nothing can be seen from a model that was not supplied explicitly. Otherwise, between the views, controllers, helpers, and whatnot, pretty much anything with an <code>@</code> (or <code>@@</code>) in front of it is visible, and any variables without are not.</p></li> </ol>
 

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