You are here: Forums Ask a Rails expert Model types/states...
Posted in Forums : Ask a Rails expert
Authority 37
Posting Rating 85
Sign in to rate this post
|
I’m wondering how other Rails experts handle states and types of model instances. For example: an Address record has a type Home, Work or Other. There are several options for solving this problem:
In my opinion, option 1. and 2. are a bit overkill. Creating more models for only three types is not very nice and can lower the overview of the models in a project. Storing a string representation is an option, but also doesn’t have my preference. The string representation is often localized for display in the views and it takes a lot of space in the database. I’m currently using the last option, combined with a constant Hash in an initializer:
When storing a new Address record the integer representing the state is stored in the database. Displaying the description of the state is done with
The code isn’t readable like this and you force developers to lookup the meaning of the ‘1’ in the hash. So my question is: how would you solve this? Note: the Address model is just an example, there are many more examples of types and/or states that should be usable in views, readable in the code and easy to store in the database. Thanks! |
Authority 37
Posting Rating 1
Sign in to rate this post
|
You could set a constant for each type: WORK = 1 But i would not do that and instead revisit my domain model. Matthias |
Authority 37
Posting Rating 85
Sign in to rate this post
|
I absolutely think your solution works for the Address case, but as I stated before: this isn’t specific about the address case. Although creating a join model for this relation might be a bit overkill because one address is not likely to be used more than once and even when it is, you don’t let other users choose from a list of already entered addresses. Working with the constants for each type doesn’t make it more clear in my opinion. Are the three constants the only one that can be used as an address type? What about other constants? This coding standard doesn’t say anything about the set from which a type should be chosen. Working with the constants is absolutely better compared to the integers. |
Authority 37
Posting Rating 100
Sign in to rate this post
|
I’d probably have a second table address_types where I store the name of the address type (“Work”, “Home”, “Holiday”, whatever). This way everything stays clear and is extendable because users can optionally add new types if need be. |
Authority 12
Posting Rating 96
Sign in to rate this post
|
To go on from Clemens’ point (using an AddressType model), you could also use the caches_constants plugin (I think I mentioned it in this forum a while ago). http://trac.extendviget.com/lab/wiki/CachesConstants . I’m using similar code in one of my models. |
Authority 37
Posting Rating 85
Sign in to rate this post
|
Thanks for the link Jon, didn’t know the plugin, but seems really useful for this situation. I agree with Clemens that taking changes in the types into account is also important. |
Ask a Rails expert : First post, requesting sage perspective
Ask a Rails expert : How to use mephisto
Ask a Rails expert : How to use mephisto
Ask a Rails expert : will_paginate customization problem
Ask a Rails expert : BackgroundRB still wants 'development' environment...?
Ask a Rails expert : activescaffold, sql exception
Ask a Rails expert : Passing non-english chars in query string
Ask a Rails expert : Rails and 2D barcodes
Ask a Rails expert : apache giving proxy error
Ask a Rails expert : Custom Responses w/ 'extra' information...?