8

I have the following in my user model

attr_accessible :avatar, :email

validates_presence_of :email
has_attached_file :avatar # paperclip

validates_attachment_size :avatar,
                          :less_than => 1.megabyte,
                          :message => 'Image cannot be larger than 1MB in size',
                          :if => Proc.new { |imports| !imports.avatar_file_name.blank? }

in one of my controllers, I ONLY want to update and validate the avatar field without updating and validating email.

How can I do this?

for example (this won't work)

if @user.update_attributes(params[:user])
 # do something... 
end

I also tried with update_attribute('avatar', params[:user][:avatar]), but that would skip the validations for avatar field as well.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Madhusudhan
  • 8,374
  • 12
  • 47
  • 68
  • 1
    Duplicate? http://stackoverflow.com/questions/457239/is-there-a-way-to-validate-a-specific-attribute-on-an-activerecord-without-instan/457313#457313 – Kevin Sylvestre Aug 23 '11 at 03:39
  • no, i think `mock.valid?` validates everything. I am looking to validate only one attribute. – Madhusudhan Aug 23 '11 at 06:22

5 Answers5

13

You could validate the attribute by hand and use update_attribute, that skips validation. If you add this to your User:

def self.valid_attribute?(attr, value)
  mock = self.new(attr => value)
  if mock.valid?
    true
  else
    !mock.errors.has_key?(attr)
  end
end

And then update the attribute thusly:

if(!User.valid_attribute?('avatar', params[:user][:avatar])
    # Complain or whatever.
end
@user.update_attribute('avatar', params[:user][:avatar])

You should get your single attribute updated while only (manually) validating that attribute.

If you look at how Milan Novota's valid_attribute? works, you'll see that it performs the validations and then checks to see if the specific attr had issues; it doesn't matter if any of the other validations failed as valid_attribute? only looks at the validation failures for the attribute that you're interested in.

If you're going to be doing a lot of this stuff then you could add a method to User:

def update_just_this_one(attr, value)
    raise "Bad #{attr}" if(!User.valid_attribute?(attr, value))
    self.update_attribute(attr, value)
end

and use that to update your single attribute.

Community
  • 1
  • 1
mu is too short
  • 426,620
  • 70
  • 833
  • 800
8

A condition?

validates_presence_of :email, :if => :email_changed?
Kleber S.
  • 8,110
  • 6
  • 43
  • 69
  • Brilliant. Thank you, I didn't even realize you got these handy helper functions! – Dan Sep 27 '18 at 18:06
1

Here is my solution. It keeps the same behaviour than .valid? method, witch returns true or false, and add errors on the model on witch it was called.

class MyModel < ActiveRecord::Base
  def valid_attributes?(attributes)
    mock = self.class.new(self.attributes)
    mock.valid?
    mock.errors.to_hash.select { |attribute| attributes.include? attribute }.each do |error_key, error_messages|
      error_messages.each do |error_message|
        self.errors.add(error_key, error_message)
      end
    end

    self.errors.to_hash.empty?
  end
end

> my_model.valid_attributes? [:first_name, :email] # => returns true if first_name and email is valid, returns false if at least one is not valid
> my_modal.errors.messages # => now contain errors of the previous validation
{'first_name' => ["can't be blank"]}
jvenezia
  • 890
  • 7
  • 10
1

Have you tried putting a condition on the validates_presence_of :email ?

http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000083

Configuration options:

if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.

unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.

Community
  • 1
  • 1
fatfrog
  • 2,118
  • 1
  • 23
  • 46
1

I am assuming you need this, because you have a multi-step wizard, where you first upload the avatar and the e-mail is filled in later.

To my knowledge, with your validations as they are, I see no good working solution. Either you validate all, or you update the avatar without validations. If it would be a simple attribute, you could check if the new value passes the validation seperately, and then update the model without validations (e.g. using update_attribute).

I can suggest two possible alternative approaches:

  • either you make sure that the e-mail is always entered first, which I believe is not a bad solution. And then, with each save, the validation is met.
  • otherwise, change the validation. Why would you declare a validation on a model, if there are records in the database that do not meet the validation? That is very counter-intuitive.

So I would propose something like this:

validate :presence_of_email_after_upload_avatar

def presence_of_email_after_upload_avatar
  # write some test, when the email should be present
  if avatar.present?
    errors.add(:email, "Email is required") unless email.present?
  end
end

Hope this helps.

nathanvda
  • 49,707
  • 13
  • 117
  • 139
  • errors.add(:email, "Email is required") generates error = "Email Email is required". Change "Email is required" to just "is required" – Filip Bartuzi Dec 04 '15 at 11:51