validation - What is the Rails Convention for validating field length? -
validation - What is the Rails Convention for validating field length? -
with activerecord models, know can validate length of input field so
class user validates :user_name, length: { maximum: 20 } end
however, 1 of design patterns in rails recommends lean models. if have ton of validations, above code might seem intimidating. read there way this.
you can utilize activerecord::schema
accomplish same task.
class createusers < activerecord::migration def alter create_table :users |t| t.string :user_name, limit: 20 end end end
that accomplishes exact same thing don't need sec line in users
model.
what standard rails convention regarding this?
some people argue have have skinny controllers , skinny models. however, can create several additional classes in application.
sometimes having fat model if documented , laid out logically can easier read. ignore 'best practices' if makes code easier read may not person touching code. if application scales point multiple people accessing same files, consider extracting @ point refactor. however, has been case.
while set limits on database, want have client validations prevent having info truncated no feedback them. example, (a horrible example), if limit username of user 6 characters , type in kobaltz
username, wonder why username/password never works database truncated kobalt
. run issues mysql (or similar) throw database level errors annoying fix/troubleshoot. have consider if modifying database in production, if set limits did not exist before, end corrupting data.
having few validations in model not create 'fat' model in opinion. makes easier read. if you're not using ide rubymine , using editor, not have luxury of jump definition
can create abstraction of model easier follow.
ruby-on-rails validation activerecord model convention
Comments
Post a Comment