This is not a blog, this is a question!
Speaking of types, I mean that we have a type value and a type description. We can have different types (gender, size, color, etc.) and a set of values โโfor each type. We have 2 options: save the types in the database or in the application.
For me, saving the entire table to store type values โโfor each type is redundant. Another way is to store types in one table with fields: type name, type value, type description. To keep the type in db useful if you want to change its values โโfrom the application. But basically, when I add a new type, I change the behavior of the application.
For me, the best choice is to save types in the application. I have a YML configuration file similar to this (this is actually part of the SettingsLogic settings file):
types:
gender: "male female"
This way I can check the input of the form:
validates_inclusion_of :gender, :in => Settings.types.gender.split(/\s/)
Since my application is multilingual, I save the descriptions in localization files:
ru:
types:
gender:
male: ""
female: ""
To prepare the selection for selection, I use this helper method:
def genders
genders = []
Settings.types.gender.split(/\s/).each do |gender|
genders << [t("types.gender.#{gender}"), gender]
end
genders
end
The disadvantage of this approach is that I have to store in db values โโof type long for men and women instead of sufficient m and f.
So I think of a hash:
def genders
genders = []
gender_types = { :m => "male", :f => "female" }
gender_types.each do |key, value|
genders << [t("types.gender.#{value}"), key]
end
genders
end
But at the moment I have no answer where to store the hash, as I do with the lines in the configuration file. As I said, this hash should be accessible not only from the helper method, but also when checking.
So the question is wide enough: how do you save types? What is the best or best approach?