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.
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
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.
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!
Tips on Adapting to Remote Work
Reducing Developer Friction With Rails 6’s Actionable Errors
What makes a good developer a great developer?
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.
Webinars are our online portal for tips, tricks and lessons learned in everything we do. Make the most of this free resource to help you become a better developer.
The Ruby on Rails Installfest includes a full setup of your development environment and step-by-step instructions on how to build your first app hosted on Heroku. Over 1,800 attendees to date and counting.
The Ruby on Rails Development Hub is a monthly event where you will get the chance to spend time with our team and others in the community to improve and hone your Ruby on Rails skills.