Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check if an attribute value has been set during the process flow?
    text
    copied!<p>I am using Ruby on Rails 3.2.2 and I would like to know if there is a way to check if an attribute value has been <em>set</em> during the process flow. That is, I have an <code>Article</code> class with attributes <code>title</code>, <code>content</code>, <code>updater_user_id</code>.</p> <p>In my controller I have:</p> <pre><code># @article.title # =&gt; "Sample title" # @article.content # =&gt; "Sample content" # @article.updater_user_id # =&gt; 1 # @current_user.id # =&gt; 1 # Note: This value is the same as '@article.updater_user_id' @article.updater_user_id = @current_user.id # params[:article] # =&gt; {:article =&gt; {:title =&gt; 'Sample title 2', :content =&gt; 'Sample content 2'}} @article.update_attributes(params[:article]) </code></pre> <p>I tried to use the <a href="http://apidock.com/rails/ActiveModel/Dirty/changed" rel="nofollow"><code>@article.changed</code></a> method but it doesn't work as I would like it to work. In fact, that method returns:</p> <pre><code>@article.changed # =&gt; ['title', 'content'] # Note: There isn't the 'updater_user_id' attribute </code></pre> <p><em>In other words, I would like to check if the <code>updater_user_id</code> has been set during the process flow even if that value has not changed.</em> My use case is that I would like to <em>require</em> that the <code>updater_user_id</code> is set (at least one time) during the process flow before to save an <code>Article</code> object; if it is not set during the process flow, then I would like to raise an error or something like that.</p> <p><strong>Is it possible? If so, how to make that?</strong></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