Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

validate and update single attribute rails

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.

like image 731
Madhusudhan Avatar asked Aug 23 '11 03:08

Madhusudhan


4 Answers

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.

like image 144
mu is too short Avatar answered Nov 01 '22 21:11

mu is too short


A condition?

validates_presence_of :email, :if => :email_changed?
like image 43
Kleber S. Avatar answered Nov 01 '22 21:11

Kleber S.


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.

like image 1
fatfrog Avatar answered Nov 01 '22 21:11

fatfrog


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.

like image 1
nathanvda Avatar answered Nov 01 '22 22:11

nathanvda