ruby on rails - Default value along with uniqueness validation -
class path & lt; ActiveRecord :: Base attr_accessible: The name validates: Name, Attendance: True, Specification: true before_validation: set_default_path Private def set_default_path self.name = name.presence || '/' End End
Domain model has been created as such:
class domain & lt; ActiveRecord :: Base attr_accessible: name, path_id Valid: name, path_id, presence: correct confirmation: name, specification: {radius: path_id} expiration
but it does not work for it Continuous inserts with a blank name for the path
path = path.find_core_bite_name ('') domain = domain.new (name: 'stackoverflow.com') domain.path = path domain.save! #Failed with verification error, ActiveRecord :: RecordInvalid: validation failed: path can not be empty
Is there a strong way to achieve this?
You should remove the following callback
before_validation: set_default_path < / Code>
and use verification for the name as the following: -
Valid: name, presence: true, specification: true, if = Gt; 'Name.present?'
And add a migration file to add the default value for the path table name attribute: -
change_column: path,: name, string, : Default = & gt; '/'
or
change_column_default: path, name, '/'
Comments
Post a Comment