This post was originally written by Robin Johnson
Also using HAML, SASS, Bootstrap, and Twitter OmniAuth...
Before we start – Here’s the GitHub code for this App…
We recently had our 3rd Couchbase San Francisco conference which, by the way, was a great success and a fantastic day / night. At the conference, I gave an 80-minute session based on the very sample application we are going to be building today. The purpose of my talk was to show how we can model JSON documents within Couchbase and utilise the full Map/Reduce capabilities to build an application end-to-end.
This blog assumes knowledge of Rails and some use of the Twitter Developer Portal. If you haven’t used Couchbase or the Couchbase-Model gem before, don’t worry, I will guide you through that part, but it is worth reading up on Couchbase-Model first!
The overall idea behind this app is that Users can post their own Vine videos, to be voted on by the public to see who has the Funniest Vine videos globally. Users must be authenticated before submitting a Vine, and we need to keep track of which Vine belongs to which user by means of referential ID’s. i.e. each Vine should have a User_ID within it’s JSON document, so we know who it belongs to.
So without further hesitation, let's dive in and see how we can build a gamified application atop-of Couchbase using Ruby and Rails. The whole idea of having a social application with a leaderboard and a competitive element is one that has been a very common use-case, but not necessarily one that has been well documented (especially for us in the Ruby community!) Now then, enough chit-chat, let’s go!
- Couchbase Server 2.0+
- Ruby 1.9.3+
- Rails 3+
Instead of posting every individual gem for our Rails app here, simply click here to view my Gemfile to see which Ruby Gems we need to bundle for our application.
1 note to make here is my choice of Ruby server. I am using Puma. This isn’t necessarily a benefit in this small use-case, more a personal habit. Puma is a threaded, concurrent webserver for Ruby that starts on port :9292 by default. You can use whichever web server you feel comfortable with, and simply start your app with ‘rails s’ or ‘rails server’.
Now, before we get started with our code, let’s go ahead and create a bucket in our Couchbase instance called ‘rvine’. It’s not necessary to worry about sizing this bucket, so depending on how much RAM you have allocated to your cluster, just give it 200mb or so.
So, let’s generate a new Rails app. We’re going to append our usual command with the ‘-O’ flag. This is to skip active_record includes. (We can’t use active_record with Couchbase, but that is why we have the Couchbase-Model gem!)
Rails new rvine –O
Now then, let’s make sure you’ve copied the Gemfile from the repo linked above. The importance of this is to make sure we’ve got both Couchbase and Couchbase-Model bundled. Couchbase-Model is an abstraction over the Couchbase gem allowing Active-Record like design patterns. It makes it super easy for us to get up and running with Rails & Couchbase. Make sure you click on the link above and read through the gem details to find out more.
When we know we have both gems in our Gemfile, run ‘bundle install.’ Once we’ve done that, we setup our rails app with Couchbase as a backend. From your rails directory in Terminal, run the command:
Rails generate couchbase:config
This will generate a couchbase.yml file in our config directory that links up our app with our database backend. Go ahead and edit that file, and enter our bucket credentials. Your config file should reflect this:
common: &common hostname: localhost port: 8091 username: password: pool: default development: <<: *common bucket: rvine test: <<: *common bucket: rvine # set these environment variables on your production server production: hostname: <%= ENV['COUCHBASE_HOST'] %> port: <%= ENV['COUCHBASE_PORT'] %> username: <%= ENV['COUCHBASE_USERNAME'] %> password: <%= ENV['COUCHBASE_PASSWORD'] %> pool: <%= ENV['COUCHBASE_POOL'] %> bucket: <%= ENV['COUCHBASE_BUCKET'] %>
Ok, so now that that’s done, we should have a Rails barebone project, that’s hooked up to Couchbase as it’s backend data store. Seem simple? That’s because Couchbase-Model makes our life 1000x easier when it comes to data modeling!