Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I separate the validations from the model

I have some very big models that I must migrate to the latest version of Rails. These models have quite a bunch of validations(User has aprox. 50 validations).

Is it possible to move all these validations in another file? Say app/models/validations/user_validations.rb. And if it is can someone provide an example, please?

like image 548
Robin Winton Avatar asked Aug 14 '14 14:08

Robin Winton


People also ask

How do you split data for validation?

Split the dataset We can use the train_test_split to first make the split on the original dataset. Then, to get the validation set, we can apply the same function to the train set to get the validation set. In the function below, the test set size is the ratio of the original data we want to use as the test set.

How do you check the validation of a model?

Best Practises of Model Validation Testing are: Dividing the data into testing and training sets. Dribbling the models by checking the validity for the different combinations of the training and test sets with the same source of data. Performing various cross-validation methods on the different sets of data sets.

How do you split a training and validation set?

In general, putting 80% of the data in the training set, 10% in the validation set, and 10% in the test set is a good split to start with. The optimum split of the test, validation, and train set depends upon factors such as the use case, the structure of the model, dimension of the data, etc.


1 Answers

You can use concerns for this:

# app/models/validations/user_validations.rb

require 'active_support/concern'

module UserValidations
  extend ActiveSupport::Concern
  included do
    validates :password, presence: true
  end
end

# app/models/user.rb
class User
  include UserValidations
end

You may need/want to namespace your concerns depending on your autoload path configuration:

# app/models/validations/user.rb

require 'active_support/concern'

module Validations
  module User
  ...


# app/models/user.rb

class User
  include Validations::User

From a style perspective you may want to think about why you have so many validations. Shunting them into a module will slim down the model file, but effectively the class still carries all that code around with it. you're effectively sweeping the problem under the carpet.

Are you using a lot of different forms with different validation requirements? If so, you can use form objects (which include ActiveModel functionality) to encapsulate the validations and processing needed for each form, taking the strain off the models.

Do your models have an insane number of fields? Maybe your user object needs to be composed from smaller objects like a profile, address, avatar, etc.

Of course, this is outside the scope of a version migration!

If you can't or don't want to use ActiveRecord concerns (which have some dependency management code that you may not want to carry around), you can use the excellent and tiny plug-in 'augmentations' or the derived gem:

https://github.com/chemica/augmentations-gem

This uses a very similar syntax and far less code. It also doesn't use the term 'concerns', which can mean something else in the OO terminology for different languages/frameworks.

like image 160
A Fader Darkly Avatar answered Oct 11 '22 21:10

A Fader Darkly