Note that there are some explanatory texts on larger screens.

plurals
  1. POMoving business rules into model
    primarykey
    data
    text
    <p>I asked a question earlier which elicited some great responses.</p> <p><a href="https://stackoverflow.com/questions/472733/how-would-you-tidy-up-this-controller-logic">Here's the earlier question</a></p> <p>On the back of some advice given there, I've tried moving the following controller logic</p> <pre><code> if params[:concept][:consulted_legal] == 0 &amp;&amp; params[:concept][:consulted_marketing] == 1 @concept.attributes = {:status =&gt; 'Awaiting Compliance Approval'} elsif params[:concept][:consulted_marketing] == 0 &amp;&amp; params[:concept][:consulted_legal] == 1 @concept.attributes = {:status =&gt; 'Awaiting Marketing Approval'} elsif params[:concept][:consulted_marketing] == 0 &amp;&amp; params[:concept][:consulted_legal] == 0 @concept.attributes = {:status =&gt; 'Awaiting Marketing &amp; Legal Approval'} else @concept.attributes = {:status =&gt; 'Pending Approval'} end </code></pre> <p>into the model, as so:</p> <pre><code>def set_status if status.blank? if (consulted_legal == true) &amp;&amp; (consulted_marketing == true) status = "Pending Approval" elsif (consulted_legal == true) &amp;&amp; (consulted_marketing == false) status = "Awaiting Marketing Approval" elsif (consulted_legal == false) &amp;&amp; (consulted_marketing == true) status = "Awaiting Legal Approval" elsif (consulted_legal == false) &amp;&amp; (consulted_marketing == false) status = "Awaiting Marketing &amp; Legal Approval" end end true # Needs to return true for the update to go through end </code></pre> <p>I am calling that from a before_save callback.</p> <p>As a default, both the consulted_legal and consulted_marketing attributes are set to false and not null, which is why I am testing for == false or true here, instead of asking </p> <pre><code>if consulted_legal? </code></pre> <p>for instance.</p> <p>However, this logic doesn't seem to be working. If I inspect the object, status is not being set to anything, ever. Can anyone spot why this might be happening? Have I got how attributes are accessed wrong in models, for instance?</p> <p>TIA</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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