Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could <a href="https://stackoverflow.com/questions/457239/is-there-a-way-to-validate-a-specific-attribute-on-an-activerecord-without-instan/457313#457313">validate the attribute by hand</a> and use <a href="http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute" rel="nofollow noreferrer"><code>update_attribute</code></a>, that <a href="http://guides.rubyonrails.org/active_record_validations_callbacks.html#skipping-validations" rel="nofollow noreferrer">skips validation</a>. If you add <a href="https://stackoverflow.com/questions/457239/is-there-a-way-to-validate-a-specific-attribute-on-an-activerecord-without-instan/457313#457313">this to your <code>User</code></a>:</p> <pre><code>def self.valid_attribute?(attr, value) mock = self.new(attr =&gt; value) if mock.valid? true else !mock.errors.has_key?(attr) end end </code></pre> <p>And then update the attribute thusly:</p> <pre><code>if(!User.valid_attribute?('avatar', params[:user][:avatar]) # Complain or whatever. end @user.update_attribute('avatar', params[:user][:avatar]) </code></pre> <p>You should get your single attribute updated while only (manually) validating that attribute.</p> <p>If you look at how Milan Novota's <code>valid_attribute?</code> works, you'll see that it performs the validations and then checks to see if the specific <code>attr</code> had issues; it doesn't matter if any of the other validations failed as <code>valid_attribute?</code> only looks at the validation failures for the attribute that you're interested in.</p> <p>If you're going to be doing a lot of this stuff then you could add a method to User:</p> <pre><code>def update_just_this_one(attr, value) raise "Bad #{attr}" if(!User.valid_attribute?(attr, value)) self.update_attribute(attr, value) end </code></pre> <p>and use that to update your single attribute.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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