Interesting Rails 4.2 changes
Nearly a month ago the Rails 4.2 beta1 was released. It contains a host of interesting changes but some are definitely more exciting than others. I've scoured through the change notes to bring you some of the more interesting tidbits that are changing, along with a small amount of commentary.
This is based off a talk I gave at the Sydney Ruby meetup in September.
Let's dive in.
AdequateRecord
Merged into master "live" on stage at RailsConf 2014 in Chicago @tenderlove has been hard at work making ActiveRecord faster. As the change notes state:
A lot of common queries are now no less than twice as fast in Rails 4.2
This was achieved by first refactoring a lot of ActiveRecord internals, and then being able to cache transformation from query -> relation -> arel -> SQL so that lots of common forms of queries can skip some of the intermediate steps. Aaron Patterson has an excellent blog post describing the changes: http://tenderlovemaking.com/2014/02/19/adequaterecord-pro-like-activerecord.html
He also spent some time during his closing keynote at RailsConf 2014 talking about these changes. The entire talk is worth watching but the relevant section starts here: https://www.youtube.com/watch?v=BTTygyxuGj8#t=1653
ActiveJob
Rails 4.2 introduces ActiveJob, a common interface to multiple background processors. It makes queueing up background tasks (such as sending emails) trivial.
# in an initializer ActiveJob::Base.queue_adapter = :sidekiq # :inline and other worse options # in app/jobs/publish_blog_job.rb class PublishBlogJob < ActiveJob::Base def perform(blog) blog.publish! end end # when you create your blog post. Hopefully with a Service/Factory object. PublishBlogJob.set(wait_until: blog.publish_at).perform_later(blog)
Also introduced is GlobalID which means we don't need to convert active record objects into id's or handle retrieving these objects during the job processing.
Interesting smaller changes
Rails 4.2 brings a host of other smaller changes. I've gone through the change-notes so you don't have to.
respond_with
moved to a separate gem.
In Rails 4.2 you won't be able to write:
# in a controller respond_with @user
in your controller actions, unless you also include the responders gem.
respond_with
can be useful but I find I don't use it with more complex apps.
config.assets.digest
is true in development
This settings means that your static assets will have the "checksum" in the name of the asset in development which can make development a little trickier, but overall means that your development environment is closer to your production environment. Also I've noticed that you get an error if you try to use an asset which hasn't been added to the precompile list which is extremely helpful.
*_path
helpers no longer available in ActionMailer
A great, and obvious, change. When you're sending an email you almost certainly want to use the *_url
helpers since these create absolute instead of relative urls.
Support for foreign keys
From the beginning Rails has treated database foreign keys with a small amount of disdain. Ideally you should keep your "business logic" in your application. Even so foreign keys are extremely useful so that you can ensure your data stays consistent and correct when you remove records. Finally Rails supports this out of the box.
If you've ever use the fantastic foreigner gem, it works pretty much exactly the same.
Support for Postgres 9.4 JSONB
In 9.4 Postgres introduced a new JSON datatype, JSONB which stores JSON in a binary representation allowing for deep indexing. This means Postgres is a better database than MongoDB for almost everyone's use cases and now Rails supports this format too.
Conclusion
Rails 4.2 looks to be an exciting release and will be the last branch of the 4 series. If you have a favourite feature, or any questions about how anything works, please feel free to leave a comment or question below!
Thanks.
Latest Articles by Our Team
Our expert team of designers and developers love what the do and enjoy sharing their knowledge with the world.
-
No app left behind: Upgrade your application to Ruby 3.0 and s...
-
A look forward from 2020
-
Testing Rails applications on real mobile devices (both design...
We Hire Only the Best
reinteractive is Australia’s largest dedicated Ruby on Rails development company. We don’t cut corners and we know what we are doing.
We are an organisation made up of amazing individuals and we take pride in our team. We are 100% remote work enabling us to choose the best talent no matter which part of the country they live in. reinteractive is dedicated to making it a great place for any developer to work.
Free Community Workshops
We created the Ruby on Rails InstallFest and Ruby on Rails Development Hub to help introduce new people to software development and to help existing developers hone their skills. These workshops provide invaluable mentorship to train developers, addressing key skills shortages in the industry. Software development is a great career choice for all ages and these events help you get started and skilled up.