Home > Blockchain >  using very long array in model validation - rails
using very long array in model validation - rails

Time:10-06

I have an array of 400 elements and I want to check if an attribute value is included in that array.

Something like: VALID_NUMBERS = [1,2,3 ... 400]

validates :number, inclusion: { in: VALID_NUMBERS)}

But I'd like to avoid having such a huge array in my model or validator.

What is the best way of storing this array in a specific file and then using it in my model or validator? Is there a convention in rails for this?

Thank you

CodePudding user response:

I typically have a file called lib/constants.rb which is a "catch-all" location for constants.

Then in config/application.rb you can have: require File.expand_path('../../lib/constants', __FILE__), then the VALID_NUMBERS constant is available everywhere.

  • Related