I was wondering if anybody knew of a way to do something like the following:
rails generate model Foo name:string, validates: {:name, uniqueness: true}
That is, whilst declaring a model generator with some attributes, work some rails magic to add your validations at the same time.
As others have said, there isn't currently a way to do this. Most of the special options for the rails generate model
command are parsed by the parse_type_and_options
method in generated_attribute.rb. The model_generator.rb will then build the model and migration files using this info.
The template for the model file that is created is model.rb. In Rails 4 this template can add in special code for belongs_to
, polymorphic
, and has_secure_password
but not code related to validations.
The template for the migration file that is created is create_table_migration.rb. In Rails 4 this can add in special options for limit
, decimal
, and precision
.