Can someone please point me to the right direction.
I'm using after_validation to compete geocoding and I wanted to know how I can tact on an or || option to an if clause.
This works as intended
class Address < ActiveRecord::Base
after_validation :geocode, if: :address_changed?
But this doesn't
class Address < ActiveRecord::Base
after_validation :geocode, if: :address_changed? || :zipcode_changed?
Thanks in advance.
If you don't want to use Thilo's Proc approach then you can add your own custom method and use that:
class Address < ActiveRecord::Base
after_validation :geocode, if: :address_or_zip_changed?
private
def address_or_zip_changed?
address_changed? || zipcode_changed?
end
end
This may (or may not) read better and is handy if you need the same compound condition on multiple validations.
: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.
So try a proc:
if: Proc.new { |a| a.address_changed? || a.zipcode_changed? }
You can do this using :if
validates :email, :unique=> true, :if => Proc.new { email.present? }
or you could also use lambda
validates :email, :unique=> true, :if => lambda { email.present? }
Related
I'm trying to create a validation statement that validates an object if service_area is present UNLESS service_area_radius==0
Here's the statement I created, which doesn't work:
validates :service_area, :presence => true, unless: "service_area_radius==0"
http://railscasts.com/episodes/41-conditional-validations
Like this:
validates_presence_of :password, :if => :should_validate_password?
validates_presence_of :country
validates_presence_of :state, :if => :in_us?
attr_accessor :updating_password
def in_us?
country == 'US'
end
def should_validate_password?
updating_password || new_record?
end
validates :service_area,
presence: {message: "Area Radius is missing."}, if: :radius_found?
private
def radius_found?
service_area_radius > 0
end
The validation for service_area will be executed if radius_found? returns true.
radius_found? will return true when the service_area_radius(attribute) hold value > 0.
Adding a custom message with message: option, when the validation fails.
Right so I have one user table with different users. I need to validate user tables on update prior to them moving onto the main site.
so far I've just done the following, is there a way I can block the validations depending on a role without doing a custom validation method, like with_options :on => :update
before_validation :check_role
if check_role = "developer" do |dev|
dev.validate :first_name, presence: true # this doesn't work btw...
end
def check_role
return self.role_type unless self.role_type == nil
end
I figure it out and this looks like the best way to do it:
class YourModel
with_options :if => lambda { |o| o.whatever == "whatever" } do |on_condition|
on_condition.validates_presence_of :address
on_condition.validates_presence_of :city
end
with_options :if => lambda { |o| o.condition_the_second == "whatever" } do |on_condition|
on_condition.validates_presence_of :foo
on_condition.validates_presence_of :bar
end
end
validates :first_name, presence: true, if: :developer?
def developer?
role == 'developer'
end
with_options :if => Proc.new {|user| user.role_type == 'developer'} do |developer|
developer.validates :first_name, :presence => true
end
I have this Project model:
class Project < ActiveRecord::Base
validates :status, :inclusion => { :in => ['active', 'closed'] }
validates :title,
:presence => true,
:length => { :in => 4..30 }
before_save :set_default_status_if_not_specified
private
def set_default_status_if_not_specified
self.status = 'active' if self.status.blank?
end
end
If I create a new object like this:
Project.create!(:title => 'Test 2', :pm_id => 1)
I get these errors: Validation failed: Status is not included in the list
But status field should get filled in before save.
That's because it validates before before_save.
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
(-) save
(-) valid
(1) before_validation
(-) validate
(2) after_validation
(3) before_save
(4) before_create
(-) create
(5) after_create
(6) after_save
(7) after_commit
You could try before_validation ?
It looks like validation happen before the before_save callbacks. Perhaps you want to try before_validation instead?
When using state_machine, how can you conditionally validate fields in the below way?
state :unlit do
end
state :fire do
if is_big_fire?
validates_presence_of :big_log
end
if is_small_fire?
validates_presence_of :small_log
end
end
It seems to just ignore the if conditions and validate everything inside the state D:
The only sort of solution I came up with was
validates_presence_of :big_log, :if => Proc.new { |fire| fire.is_big_fire? }
But this gets nuts if there are more validations.
validates_presence_of :big_log, :if => Proc.new { |fire| fire.is_big_fire? }
validates :fire_epicness_rating, :inclusion => { :in => %w(epic whowa RUNFORTHEHILLS) }, :if => Proc.new { |fire| fire.is_big_fire? }
etc
Is there some nice way of neatly wrapping these in if blocks?
Grouping validations thanks to with_optionsis really neat. See here.
Here's an example using the with_options for group validation.
with_options :if => :driver? do |driver|
driver.validates_presence_of :truck_serial
driver.validates_length_of :truck_serial, :maximum => 30
end
def driver?
roles.any? { |role| role.name == "driver" }
end
Source: http://rubyquicktips.com/post/411400798/conditional-validation-using-with-options-to-improve
I'm working on a model that has two associations that need to be set when an object is created, EXCEPT in one case.
Basically, it needs to work like this.
class Example < ActiveRecord::Base
has_one :foo
has_one :bar
validates_presence_of :foo
validates_presence_of :bar, :unless => :foo == Foo.find_by_name('ThisFooDoesntLikeBars')
end
I'm not sure how to build the :unless condition here, as it needs to check whether :foo is a specific object or not.
How do you do something like this?
:unless accepts a Proc
validates_presence_of :bar, :unless => Proc.new { |ex| ex.foo == Foo.find_by_name('ThisFooDoesntLikeBars') }
: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.
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html
How about the following:
class Example < ActiveRecord::Base
has_one :foo
has_one :bar
validates_presence_of :foo
validates_presence_of :bar, :unless => Proc.new { |example| example.foo == Foo.find_by_name('ThisFooDoesntLikeBars') }
end