I have several different fields that store different phone numbers in my rails app as a string. I can use the built-in rails format validator with a regex expression. Is there a way to place this expression in a helper method for all my phone number validations instead of having to open each model file and paste the expression string.
4 Answers
You can require any file from application.rb:
# application.rb
require Rails.root.join "lib", "regexes.rb"
# lib/regexes.rb
PHONE_NUMBER_REGEX = /your regex/
Then you simply use the constant wherever needed
You can alternatively make use of the built in autoload functionality of Rails, for example with the concern approach the other commenter laid out - the concern file is autoloaded by default, as are models, controllers, etc
Loading custom files instead of using the Rails' defaults might not seem idiomatic or the "rails way". However, I do think it's important to understand that you can load any files you want. Some people autoload the entire lib/
folder and subfolders (see Auto-loading lib files in Rails 4)
Another alternative is to place your code somewhere in the config/initializers
folder, these files are automatically loaded at startup and you can define shared classes/modules/constants there

- 26,189
- 9
- 66
- 118
-
While there are several good solutions here, I used this one because I use this same regex string to reformat my phone numbers using the capture method before the record saves. This solution allow me to use the same constant in the format validation and the reformate function. If you are using a regex string just to validate format then I would recommend the custom validator from engineersmnky. – Brian Tatum May 07 '19 at 16:47
Add a custom validator app/validators/phone_validator.rb
class PhoneValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value.to_s =~ /YOUR_REGEX_HERE/
record.errors.add attribute, 'must be a valid phone number'
end
end
end
Then in your models
class MyModel < ApplicationRecord
#phone: true tells it to use the PhoneValidator defined above
validates :phone_number, presence: true, phone: true
end

- 25,495
- 2
- 36
- 52
-
This is also a good answer, but the answer I marked fit my use case a little better. See comment on answer for more info. I have also upvoted this answer. – Brian Tatum May 07 '19 at 16:50
-
@BrianTatum nothing prohibits you from using both options you can just use your constant in the `PhoneValidator`. In my opinion `phone: true` beats typing `format: {with: PHONE_NUMBER_REGEX}` multiple times – engineersmnky May 07 '19 at 17:14
-
very good point it would clean thing up a lot and be less to type. – Brian Tatum May 08 '19 at 21:20
One way to do this is to create a concern that will be included in each model that has a phone number. In models/concerns
, create a new file called something like phonable.rb
.
# models/concerns/phonable.rb
module Phonable
extend ActiveSupport::Concern
VALID_PHONE_REGEX = /\A\+?\d+\z/ # Use your regex here
end
Now include the concern like this:
# models/my_model.rb
class MyModel < ApplicationRecord
include Phonable
validates :phone, format: {with: VALID_PHONE_REGEX}
...
end
Now that you have a Phonable
concern, you can put any other phone-number-related logic here as well, such as parsing and the like. The advantage of this approach is that all your logic related to phone numbers will be available for use in the models that need it, and none of that logic will be available in models that don't need it.

- 5,103
- 1
- 15
- 32
You can put it in ApplicationRecord (application_record.rb)
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
VALID_PHONE_NUMBER_REGEX = /\d{10}/ # your regex here
end
And then you can use it in any model that inherits fro ApplicationRecord

- 17,217
- 6
- 62
- 70