Reputation: 2907
I'm new to Rails, and am following this tutorial
I creates a simple model called HighScores.
I would like to customize this so that I can add a validation method for the score. I know there are shortcuts like validates_ that we can use, but for the purpose of learning, I'd like to write a method that ensures the score is between a certain range.
Where should the validate method go? In models/high_score.rb
or in controllers/high_scores_controllers.rb
? Or maybe in `/helpers/high_scores_helper.rb?
Upvotes: 1
Views: 1582
Reputation: 40277
Validation that the model has correct data should go in the model itself. This ensures that any future attempt to save the model's data will use this validation, regardless of the path taken.
models\high_score.rb
Also -- FWIW, the validates methods aren't short cuts, they are well tested code that you should embrace and use.
Upvotes: 4
Reputation: 356
The validation should go in models.
Here is an example of a range validation:
validates :score, :numericality => { :greater_than => 0 }
validates :score, :numericality => { :less_than => 100 }
Upvotes: 4