ruby on rails - Default value along with uniqueness validation -
Domain model has been created as such: but it does not work for it Continuous inserts with a blank name for the path Is there a strong way to achieve this? You should remove the following callback and use verification for the name as the following: - And add a migration file to add the default value for the path table name attribute: - or
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
class domain & lt; ActiveRecord :: Base attr_accessible: name, path_id Valid: name, path_id, presence: correct confirmation: name, specification: {radius: path_id} expiration
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
before_validation: set_default_path < / Code>
Valid: name, presence: true, specification: true, if = Gt; 'Name.present?'
change_column: path,: name, string, : Default = & gt; '/'
change_column_default: path, name, '/'
Comments
Post a Comment