I have a model where I want a few specific choices for a dropdown to set
a variable, very much like this:
How to have a drop down <select> field in a rails form? - Stack Overflow…
I’m trying to work out why all the answers to this question involve
creating a new model and db table just for the three choices outlined.
Would it not make more sense just to define the choices in the User
model and have a column in the User db table with the chosen one saved
there (assuming there are only three choices, which are very unlikely to
change and the user will only have one at any one time).
I assume I am missing something important here…
I don’t think you’re missing anything. If all you’re trying to do is
constrain a value to a finite number of choices, then what you’re
describing will work fine.
The SO responses appear to be describing a scenario where your select
options are dynamically created by user input. If that isn’t your
scenario, then it’s probably overkill.
Hi,
On Sun, May 8, 2016, at 20:01, Johnny S. wrote:
change and the user will only have one at any one time).
I assume I am missing something important here…
At least two answers suggested using constant/variable in model. There’s
also alternative of using enum [1].
[1] ActiveRecord::Enum
Hi Ryan,
yes that’s about it - the choices I want to have will not be created by
user input (I don’t think they are in the SO example either - I think
the OP only wants to allow the three choices he specified, which is why
I couldn’t work out why the answers seemed to overcomplicate things so
much…). Anyway - my choices will be defined, not user created and only
4 possibilities.
Its looking like I will just define an array with 4 possibilities in
that case…
Hi Nanaya,
I saw that, but couldn’t work out why the other answers made things so
complicated - I thought maybe I was not getting something important that
the extra model and db table allows.
I hadn’t considered Enum - looking into it now…