mardi 5 mai 2015

Ruby on Rails generate ActiveRecord model with settings

I need to create model by generating in terminal with command
rails generate model ...
How can i set validation of uniqueness on one of column, and name of primary_key field using that command? I found how could it be done by
rails g model model_name field_name1:type:uniq field_name2:type:primary_key...
- it sets for the table in database primary_key and uniqueness on certain fields with the migration. But i want to define settings for the ActiveRecord model which will be stored in "ModelName.rb" file in "models" folder of my project. For now i call
ModelName.validates_uniqueness_of(:field_name)
method every time when I operate with records of model to prevent adding record with duplicate values on :field_name1

Aucun commentaire:

Enregistrer un commentaire