Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can add private methods to your controllers and call them within your action methods:</p> <pre><code>class MyController &lt; ApplicationController # snip ... def my_action @variable1 = get_variable1() @variable2 = get_variable2() @variable3 = get_variable3() @action_specific_variable = Model4.where(my_condition) end def my_other_action @variable1 = get_variable1() @variable2 = get_variable2() @variable3 = get_variable3() @action_specific_variable = Model5.where(my_other_condition) end private def get_variable1() return Model1.where(some conditions) end def get_variable2() return Model2.where(some other conditions) end def get_variable3() return Model3.where(some third conditions) end end </code></pre> <p>If you need the logic to get these variables to be available across controllers, create a new utility module in you <code>lib</code> folder. For example, you might make a file <code>lib/utilities.rb</code> that contains</p> <pre><code>module Utilities def self.get_variable1() return Model1.where(some conditions) end def self.get_variable2() return Model2.where(some other conditions) end def self.get_variable3() return Model3.where(some third conditions) end end </code></pre> <p>and then your controller would look like</p> <pre><code>class MyController &lt; ApplicationController # snip ... def my_action @variable1 = Utilities::get_variable1() @variable2 = Utilities::get_variable2() @variable3 = Utilities::get_variable3() @action_specific_variable = Model4.where(my_condition) end def my_other_action @variable1 = Utilities::get_variable1() @variable2 = Utilities::get_variable2() @variable3 = Utilities::get_variable3() @action_specific_variable = Model5.where(my_other_condition) end end </code></pre>
 

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