How to skip before_create on particular controller alone in rails
example
class User < ActiveRecord::Base
before_create :validate_email
def validate_email
----
end
end
I want this to be skipped on this controller alone,
class FriendsController < ApplicationController
def new
end
def create
end
end
It's a hack, but you could add a virtual attribute to the model class that simply acts as a flag to indicate whether the callback should run or not. Then the controller action can set the flag. Something like:
I'm not sure off the top of my head if the
before_create
callback's:unless
option can refer directly to the name of a virtual attribute. If it can't then you can set it to a symbol that's the name of a method within your model and simply have that method return theskip_validation
attribute's value.Use method which not calls the callbacks. such as save(false), update_attribute.
Something like following
EDITED
Try this
&
Don't skip validation entirely with @user.save(false), unless that's the only thing you are validating is the email address.
You should really be moving this logic into your model with something like
What logic or difference in functionality does this controller contain compared to your other ones? Can you post your other controllers that you wouldn't like it to validate on and then we can compare it to this one.