Install Ruby on Rails in Windows —and Postgres, and Mongo, and Redis...

Googling "setting up rails on Windows", this is the second result, by @erikjansson
After spilling half a glass of water on my work Macbook Air, I set out to setup Rails on my PC once and for all. Having tried solutions like Nitrous.io to varying levels of success, I decided that noting would cut it like the real deal.

A Social Network In Rails: a gem by gem guide

I'm writing an in-depth guide on building a social network with Rails, one gem at a time. Here's what I've got so far, would love to have some feedback!

  1. 5-minute Setup with RVM and Railsbricks
  2. Photo upload with Refile (coming soon)
  3. Testing: Fixtures
  4. Elegant permalinks
  5. Activity Feed
  6. Comments
  7. Notifications (coming soon)
  8. Follow/mention people (coming soon)
  9. ...
I'm compiling and expanding these blog posts into a book via Leanpub, A Social Network in Rails, subscribe to be updated of its progress here.

Fixtures in real life, part 1

If you came here looking for these kind of fixtures, good luck on the way out ;)

This post is part of an ongoing feature about creating a social network in Rails.

There's lots of posts around about Rails fixtures, with critiques ranging from "[they're] great!", "they hurt a little early, but shine with complexity", all the way to "do not use fixtures". Few topics split the Rails community more, the eternal Rspec vs Minitest debate comes to mind.

However, something that I've found lacking in most of these fixtures posts are insights of:
  • how fixtures fare in a real life app,
  • how do they coexist with factories and plain objects, and
  • what growth strategies to follow that will help you out in the long run. 
That's what we're about to tackle on this post.

Get insights of your PostgreSQL database with PgHero


PgHero is a ruby gem that gives you insights of your PostgreSQL database.
Out of the box it provides info about stuff like cache hit rate, which queries are too slow, which tables need indexes, and more. You can see above how it looks on a production app (Yeah, I have to add an index there I know :P).

To add it to your app: add this line to your  Gemfile:
gem 'pghero'
And add this to config/routes.rb to mount the dashboard:
mount PgHero::Engine, at: "pghero"

You'll also want to secure the access to the dashboard if you mount it as part of your public-facing app; we didn't have to as it is in our admin section.

There's also the option to check the insights programmatically, run it via SQL without the web interface, run it as a standalone app, get system stats like CPU usage… check their readme for the good stuff.

Happy optimizing!

A Social Network in Rails: Comments

This post is part of an ongoing feature about creating a social network in Rails.

This time, we're adding comments to our Photo model, as presented in the elegant permalinks post. First of all, I looked for a gem to do this job for us, and found acts_as_commentable. Its setup goes something like this:

Add the gem to our Gemfile, and run `bundle install`:
gem 'acts_as_commentable'
Generate the tables and migrate the DB:
$ rails g comment
$ rake db:migrate
Add the comments to our Photo model:
class Photo < ActiveRecord::Base
  acts_as_commentable
end
By doing this, we get the following interface to the comments in our photos: