Note that there are some explanatory texts on larger screens.

plurals
  1. POMoving logic from controller to model in rails 3?
    text
    copied!<p>I've been building a contest application, and I can easily tell I've been putting wayyyy too much logic in the controller. How can I go about switch this type of logic to the model? (whats important here isn't the logic itself - its far from finished - I'm just trying to understand how to move it out of the controller).</p> <p><strong>Controller:</strong></p> <pre><code>def create @person = Person.new(params[:person]) @yournum = rand(100) @day = Day.find_by_id(1) @prereg = Prereg.find_by_email(@person.email) if @preg != nil @person.last_name = @prereg.name end if @day.number == 1 if @yournum &lt;= 25 @person.prize_id = 2 elsif @yournum &gt; 25 &amp;&amp; @yournum &lt;=50 @person.prize_id = 1 elsif @yournum &gt; 51 &amp;&amp; @yournum &lt;=75 @person.prize_id = 3 elsif @yournum &gt; 76 &amp;&amp; @yournum &lt;=100 @person.prize_id = 4 end elsif @day.number == 2 if @yournum &lt;= 25 @person.prize_id = 2 elsif @yournum &gt; 25 &amp;&amp; @yournum &lt;=50 @person.prize_id = 1 elsif @yournum &gt; 51 &amp;&amp; @yournum &lt;=75 @person.prize_id = 3 elsif @yournum &gt; 76 &amp;&amp; @yournum &lt;=100 @person.prize_id = 4 end elsif @day.number == 3 if @yournum &lt;= 50 @person.prize_id = 2 elsif @yournum &gt; 51 &amp;&amp; @yournum &lt;=90 @person.prize_id = 1 elsif @yournum &gt; 91 &amp;&amp; @yournum &lt;= 95 @person.prize_id = 3 elsif @yournum &gt; 96 &amp;&amp; @yournum &lt;=100 @person.prize_id = 4 end end @person.save redirect_to @person end </code></pre> <p><strong>Model:</strong></p> <pre><code>class Person &lt; ActiveRecord::Base belongs_to :prize end </code></pre> <p>Thanks!</p> <p>Elliot</p>
 

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