With Rails 4.1 released last month, we've got some new features to leverage in our applications. Here are some that are definitely worth checking out.
Let's say we have a user model where each user has a state attribute. I want to find those users who don't live in NY.
Previously, that query might look something like this.
User.where("state != 'NY'")
With where.not we can now write it more simply.
In some of your controllers, you may start building a query with a base AREL object. Based on certain parameters, that query may requiring changing one of the base where clauses.
For example, here is an index action that by default does not show archived funds.
def index @funds = Fund.where.not(archived: true) @funds = @funds.rewhere(archived: true) if params[:show_archive]
Note: If another where clause is present, it will still be present. Only the criteria specified in the rewhere overwrite.
This feature now accepts a hash argument where you can specify from and to. For example, you can check to see if a subscription as changed from active to cancelled.
person.name_changed?(from: "Uncle Bob", to: "Bob") # => true
I'll be writing more in-depth articles regarding enums and the mailer preview which are also new in Rails 4.1. It's refreshing to see Rails continue to move forward with features that are useful to all that use the framework.