You can. But first, my assumptions:
- I'm going to assume that you are using
ActiveRecord
and you have a model calledMyModel
in order to treat that table. - I'm going to assume that you used the proper way to make fields non-nullable, which is by using validations. If this is not the case, I strongly suggest that you reconsider using them.
Then you can do:
class MyModel < ActiveRecord::Base
validates_pressence_of :name, :if => :check_name
attr_writer :check_name
def check_name # make it defalt to true
@check_name = @check_name.nil? ? true : @check_name
end
end
You can use it like this:
my_object.name = "Josh"
my_object.save # ok
my_object.name = ""
my_object.save # not ok
my_object.check_name = false
my_object.save # ok
egarcia
2010-07-26 14:54:08