Skipping validation on create method - ruby-on-rails

I need to skip the validation on create method.I am using Rails 4 and ruby 2
i have tried like this
#model.save(:validate => false)
it not working

Assuming you are talking about ActiveRecord; In Rails 3 and 4 the way to skip validations and potentially persist invalid objects is as you describe:
#model.save(:validate => false)
In Rails 2 you'd need to do
#model.save(false)

You can do this in model by
validates :some_attr, :presence => true, :on => :update

validates :some_attr, :presence => true, :unless => :create
skips the validation JUST for create.

Related

allow validations only when resource added from activeadmin rails

I have a form in active admin for user. I have added a validation 'presence' for the below attribute,
validates :needy_id, presence: true
I want it to work only when the request comes from activeadmin.
Any suggestions?
Please check this,
validates :needy_id, :presence => true, :if => Proc.new { user.admin? }

Validation fails on ":on => :save"

I am using rails 3.0.10 and ruby 1.9.2p0
In my rails app I am trying to validate my model through a "validates_uniqueness_of"
In my model(label) there is field named as "name" and I want to validate it as:
validates_uniqueness_of :name, :scope => [:portal_id], :on => :save
Portal is another model in my app.
It validates perfectly when i use :on => :create or :update individually, but fails when i use :on => :save
any idea I want to validate on save, but AR validation fails.
As default all validations run before save.so no need to give :on => :save
Simply use
validates_uniqueness_of :name, :scope => [:portal_id]

Rails 3 & validate_on_create

Rails 3 warns me that validate_on_create is deprecated. I have def validate_on_create in one of my models. What do I replace it with?
You can use:
validates :name, :presence => true, :on => :create
If you have a method that performs validations, you should run as:
validate :method_name, :on => :create

Rails model conditional vaidation

I validate fields in model using:
validates :first_name, :presence => true, :if => :should_validate?
validates :last_name, :presence => true, :if => :should_validate?
...
There are many fields in model that needs to be validated and it doesn't look good if I specify :if => method for each one.
Is it possible to embed this validates methods in block instead of giving :if => method for each one?
You could write your own custom validator of course, but if you're only validating presence, this might do the trick:
validates :first_name, :last_name, :presence => true, :if => :should_validate?
I don't think there is something out of the box for this. If you want, you can use a custom validator.
What are the conditions that you need this validated? If you don't need it validated couldn't you just leave that line out? Otherwise you could just validate on certain actions so you don't need to evaluate for should_validate?, for example:
validates :first_name, :last_name, :presence => true, :only => [:create, :update]

validate email in rails after updating

In my user model, I have
validates :email, :presence=>true,
:format => { :with => email_regex },
:uniqueness => true
In my controller, I update the email if a user chooses to change it like this:
#user.update_attribute("email","#{#new_email}")
However, it doesn't throw an error if the format is not honored.
update_attribute does no validations. use
#user.update_attributes({ :email => #new_email })
instead.
I found out that update_attribute skips checking for validation but update_attributes doesn't! Interesting.
http://apidock.com/rails/ActiveRecord/Base/update_attributes

Resources