Primary Menu

I recently deployed a Rails 4 app with ElasticSearch to Heroku and ran into a couple of snags along the way. I’m hoping this tutorial will be useful for others running into the same issues. Leave a comment if you have any problems or have figured out a better way! Good luck!

If you run into any problems or error messages at this point, make sure that you’ve run a migration for your Heroku app:

$ heroku run rake db:migrate

And then run the elasticsearch:import command again.

Finally restart your app:

$ heroku restart -a

And that’s it! Elasticsearch should now being running on your Rails 4 app deployed on Heroku. Test it out by first adding a few posts by navigating to http://.herokuapp.com and then visit http://.herokuapp.com/search to see Elasticsearch in action.

Do you ever find yourself fighting with CSS conflicts in the asset pipeline? Heres’s a handy namespacing trick mentioned by Brian Morearty in his Rails 4.1 Performance Fundamentals course. You can easily apply style rules to all pages of one controller, or specifically target a single action in a controller, by adding the current controller and action as class names to the body tag in your application.html.erb view file:

I was recently working on a Rails app that required role-based authorization for its users. Basically, the application would need the ability to check to see if the user had the proper credentials to access certain features of the site.

I’ve typically handled this in the past by either adding an admin boolean column in the Users table so I could simply call @user.admin? to check to see if the user is an admin or not, or for situations which require multiple levels of authorization (or the ability to give a user more than one set of privileges at a time), adding a many-to-many association between the User model and another model called Role. This many-to-many association also requires a join table called role_user. The latter approach is obviously more complex and requires the creation of several helper methods to become convenient to use.

Since I was using Rails 4.1 for this project and because each user only needed to be granted one user role at a time, I figured I would give the new ActiveRecord::Enum feature a try. Rails now provides a really simple way to quickly add attributes and a bunch of associated helper methods to an Active Record class.

The only additional thing you need to get these Active Record enums working is a migration to add the attribute to the model, in this case adding role to User. Notice that this column is an integer value, which is mapped to the index of each enum value:

You may already know that if you want to check for the existence of an Active Record object you can use a method called exists? Let’s say you have a User model and would like to know if a record exists with a matching primary id:

User.exists?(1) # returns true if a user with matching id of 1 exists and false otherwise.
User.exists?(:name => 'John') # also works with find-style conditionals

It’s also possible to use exists? without any arguments on a model to see if the table is empty:

User.exists? # returns false if the users table is empty and true otherwise.

Additionally, you can use User.any? and User.many? instead of exists? to check for existence on a model or relation.