Rails 4.1 introduced ActiveRecord Enums, a handy little feature that lets you store an integer in a database column, but use symbols or strings in your code. Until today, I’ve been presenting these in select boxes with code like this:
This generates a perfectly acceptable select box if you don’t need internationalization and the enum names you’ve picked are good enough to present to the user. If thats not the case, or you to change the presentation without changing the code, a new helper method leveraging Ruby’s internationalization (i18n) features can be a good approach.
Keep reading for how I changed the presentation of enumerations in a drop down without changing my models or renaming the enumeration names.
Here is how we can provide alternate labels for some of our drop down values.
First, create a new helper that will pass each enumeration name through i18n to perform a label conversation
Second, change your form to call the helper
Third, provide the translations.
A few notes:
- If the translation is missing, the code’s default is to fall back to the titleized text. If you want to omit options where the translation is missing, You can modify the methods to call enum_to_translated_option with a default of blank and the blank options will be removed from the select.
- enum_to_translated_option is useful in a show template.
- It may be useful to sort the options after translation. That is an easy addition to the helper.
Is this useful for you? Is there a better way? Let me know!
Rails 4.0 introduced a small bug in JSON generation with this pull request. The output format for times (ActiveRecord::TimeWithZone) in JSON changed to include milliseconds. Sounds good right? Well, not if your API clients crash trying to parse milliseconds. Unfortunately, Rails 4.0 didn’t provide a configuration option for the timestamp precision in JSON output. What is a programmer under the gun to do? Upgrade to Rails 4.1 or get out your monkey and your patch and get to work?
First, lets see the bug in action. This test passes in Rails 3.2.15, but fails in Rails 4.0.4:
Simple enough. This is easier to see if you’ve got a Rails app:
Rails 4.1 adds a configuration option to default the time precision. In that version, you can set ActiveSupport::JSON::Encoding.time_precision = 0. However, if you are on Rails 4.0 for a bit, you can monkey patch AtiveSupport::TimeWithZone to go back to the Rails 3.2 implementation:
Normally, it seems the Rails team does a better job than this. Fortunately, with Ruby we’ve got the openness to go back to the old behavior in a pinch.
Upgrading from Rails 3.2 to Rails 4.0 is not a trivial task. Sure, there is a guide, but when you upgrade you’ll probably be upgrading a lot of your gems, maybe your jQuery and jQuery-UI versions and then there are all the undocumented unintentional changes that can cause you grief. If you’ve got a good test suite (you have one right?) you’ll catch a lot of these, but some will leave you scratching your head. This one tripped me up for a while: we had a very simple scope stop working in Rails 4.
The scope model below works in Rails 3.2, but fails in Rails 4 when used with an association:
Here is the failure:
In the second query, post.comments.model.count the query has lost the comments scope as well as the conditional defined in our model scope and it counts all comments in the system.
The fix is quite easy, just change the name of the scope to something like not_index.
This seems to be change in associations that is most clearly illustrated by this:
I could never quite figure out where in activerecord this changed, but model was not a great choice for a scope name. This executable gist has everything in this post if you want to explore this more.
What surprises have you found with Rails 4?