database design - What is the proper way to handle "types" in Rails? -
Sorry, if asked before this question, I searched but I do not believe in the correct name for this. Was there. for.
I am a newcomer to rail and development, I have done some hacking programming but I will not say that I know what I am doing. I'm always at a disadvantage in my rail models and in general the way to define "type" in the database. For example, say that I have a model "car" and it has the property of "color", where the color is chosen from a known set rather than any RGB value, or whatever it tells me to color There should be another table (and associated model) and then there are some relations between the two, but which relationship is appropriate? A car does not have its color, or vice versa.
Or of course, I can only store an integer and see it in code, but it seems wrong to me.
What did I remember? :)
Color can be a complex object or can be very easy, its requirement May be on the basis.
- Simple: The integer has changed to any other format, or hex value such as "a3ab34" stored in a string.
- Premises: For some people you headline.
For simpler you will have a column in named cars, and override the reader method to return the color format you want , like.
If we are talking about a complex case, then it is different then you should have a different model called color. So the car
will be related_ to: color
as thenduks said above. To be more accurately meaningful, the car has_one: color
will be meaningful, but then you are compelling the color table to store car_id
, which will be the case behind our case In, the car should be accumulating color_id
, so make sure you create a migration that adds color_id
to table car
Then you should create a migration for colors
table, for example id
, title
and code
are included, and which you must fill in yourself by using guardrail ceding accessories. (See)
Comments
Post a Comment