CouchDB on Rails (part 6 of ?)

  1. An introduction to CouchDB
  2. Installing CouchDB
  3. Experimenting with CouchDB’s web interface
  4. Integrating with Rails using ActiveCouch
  5. Integrating with Rails using RelaxDB
  6. Getting to scaffolding using RelaxDB
  7. Installing CouchDB from Subversion source code
  8. Trying out couchrest and topfunky’s basic_model

Okay, after my brief disappointment earlier, i’ve found a way that seems to work, at least for the time being. I have taken the RelaxDB lines out of environment.rb and put them into the application controller instead.

# app/controllers/application.rb
class ApplicationController  'localhost', :port => 5984)

Oh, i should say, i’m going to add the RESTful resources command into config/routes.rb:

map.resources :cds

I have also added some of the other properties into the model:

class Cd < RelaxDB::Document
  property :title
  property :artist
  property :publisher
  property :year_of_release

It’s a shame i have to do this. I’d like to see a wrapper for CouchDB which doesn’t try to force a schema upon the database. That is one of CouchDB’s biggest strengths – that documents can contain any fields you wish.

Index of all CDs

I know my boss will probably read this, so i’ll do the right thing and start with a spec!

# spec/controllers/cds_controller_spec.rb
describe CdsController do
  describe "index" do
    it "should look up all CDs in the database" do
      get :index
      assigns[:cds].should == [:some_cds]

And the code to make this pass is simply going to be:

# app/controllers/cds_controller.rb
class CdsController < ApplicationController
  def index
    @cds = Cd.all

The spec passes. Let’s write the view! Because RelaxDB wraps up the database so nicely for us, there is nothing unusual here at all.

My extensive CD collection

Title Artist Publisher Year of release

Rails reading CouchDB!

Now we are definitely getting somewhere! :) But the CDs appear in the order they were created. To order them by artist then title, we can do this:

@cds = Cd.all.sorted_by(:artist, :title)

You know what RelaxDB is going to do now?

[info] [] - - "GET /cd_collection/_view/Cd/all_sorted_by_artist_and_title" 200

Yep, it dynamically creates the views it needs. Clever, hey?! And here’s the output:

CD collection sorted by artist then title

Differences between ActiveRecord and RelaxDB

I’m not going to go through every line of code, but here are the main differences from ActiveRecord:

To find all CDs (as i already demonstrated):

# @cds = Cd.find(:all, :order => "artist, title")
@cds = Cd.all.sorted_by(:artist, :title)

To find a CD from the parameters:

# @cd = Cd.find(params[:id])
@cd = RelaxDB.load(params[:id])

Updating attributes – there is no method ‘update_attributes’. There is ‘set_attributes’ but it doesn’t actually save the document – you have to do that yourself.

# @cd.update_attributes(params[:cd])

Destroy requires an exclamation mark.

# @cd.destroy

Validations are rubbish. I managed to get it sort of working like this:

property :title, :validator => lambda {|t| t != ""},
  :validation_msg => "cannot be blank"
property :year_of_release, :validator => lambda {|p| p.to_i > 0},
  :validation_msg => "must be a number" 

And then in the view you have to do something like this:


… it’s very flaky though. Sometimes it lets it through even when the validation blatantly fails. Still, it’s a known limitation.

My verdict on RelaxDB

I like it. I am glad that i had to change very little in the views to get it to work, and relatively few things in the controller. Apart from the validation and error messages, it’s a good little plugin if you want to get going quickly on CouchDB. I’d like to see it allow dynamic fields on documents … that would be a good improvement.

I may work a bit more with RelaxDB … it would be good to find out how to link two models. Watch this space! :)

Part 7: Installing CouchDB from Subversion source code


19 comments on “CouchDB on Rails (part 6 of ?)

  1. Hi Aimee

    I’ll echo what Jan said – this is a great series.

    I’m glad you got RelaxDB running on rails. I’m sorry I can’t offer any advice as to where you should configure the connection, but you know more than I do at this point!

    (I’ve pasted code related to this reply on

    > It’s a shame i have to [add properties to a model]

    That’s interesting, I hadn’t considered the need for adding properties dynamically. You could do it with module_eval or instance.class.instance_eval but I’m guessing that’s not quite what you were looking for.

    > To order them by artist then title, we can do this:

    You’ve probably also seen that sorted_by yields a query object that maps directly to CouchDB query params.

    > Sometimes it lets it through even when the validation blatantly fails.

    Hmm, that’s not so good. If you have an example of where it fails, please send it on.

    Looking forward to the next installment.

  2. Hi Paul, thanks for your thoughts. I do think that dynamic properties are a major strength of CouchDB. I guess you have played with the web interface that comes with CouchDB? You can just add as many fields as you like, with any name you like. Each document can have an entirely different set of fields, if it wishes.

    I think the couchrest plugin allows dynamic fields without any need for module_eval.

    The validation fails as follows. Here is the model:

    class Cd < RelaxDB::Document
      property :title, :validator => lambda {|t| t != ""}, :validation_msg => "cannot be blank"
      property :artist, :validator => lambda {|t| t != ""}, :validation_msg => "cannot be blank"
      property :publisher
      property :year_of_release, :validator => lambda {|p| p.to_i > 0}, :validation_msg => "must be a number"

    If i try to save a new empty CD it gives me all three error messages. If i then fill in just the year_of_release, leaving title and artist both blank, it saves.

    If i remove the year_of_release validation (so that it just validates title and artist) then it works as expected and doesn’t let me save until i enter both a title and an artist.

    To be honest, maybe i got the validation wrong. I wrote it last night when i was tired and in a hurry to finish the blog post. Now that i look at it, i realise that it is not actually converting the value at all. So if i enter “123bubbles” it will probably pass the validation but store “123bubbles” in the database! Oops! How do you recommend validating a number?

  3. Thanks for the example. As you observed, validation isn’t great.

    The reason validation wasn’t working for title or artist is because nil != “” is true. You probably want something like lambda {|t| not t.blank? } for your strings.

    To ensure that a string gets converted to a number, things get ugly :)
    before_save lambda { |o| o.year_of_release = o.year_of_release.to_i }
    property :year_of_release, :validator => lambda {|p| p > 1908}, :validation_msg => “must be > 1908”

    I’ll probably add something like
    property, :foo, :is =>Fixnum
    over the coming week, but I want to think about how that ties in with callbacks and validation first.

    If you really want to dynamically define properties, one approach would be to subclass RelaxDB::Document and define method_missing so it invoked property on the class.

  4. I played around with CouchDb (via CouchDBX on the Mac) and RelaxDb. I got it working very quicky, thanks to Aimee’s excellent walk-through and the total ease of running CouchDBX on the Mac.

    I didn’t try validations, so I can’t comment on that aspect of it.

    One thing that I noticed was that properties are not inherited via subclasses. That would be a nice feature in my opinion.

    I also see both sides of the argument, concerning a schema-less model vs the need to define properties in the model (ala DataMapper).

    I think CouchDb is fantastically powerful and full of potential. However, I can’t wrap my mind past the views and the reduce. However, seeing how RelaxDb uses those views to make some easy to use relationships, properties, and searching for those properties, makes a lot of sense.

    There’s an interesting balance to find in RelaxDb. Easy to use for the Rails ActiveRecord guys (like me) versus the flexibility available to CouchDb (via map/reduce and views). Personally, I’d love to throw a whole bunch of hash like structs into couchdb, and then search, modify, and aggregate those documents, without having to wrap my mind around map/reduce. But, that’s just me.

  5. @Paul, wow, it’s great to have you following along and responding to the particular issues that i’ve encountered. Thank you for the helpful hints about validations. That makes perfect sense to me. The ability to ensure properties are of a particular type will be very useful, i am sure.

    @Randy, great! I’m so pleased that you found my notes helpful and got things working okay. To get me started i want something similar to ActiveRecord, and RelaxDB was certainly a good help. Next i’m going to move on to the Ruby libraries couchrest and couchobject, which i think will give me a little more flexibility.

  6. Hi Aimee. I haven’t seen a example of new or edit template. I’m very curious about how to create forms with relaxdb, since the usual way is not working

  7. Hi Aimee,you had this problem?>> Cd.allRuntimeError: 500:Internal Server ErrorMETHOD:GETURI:/cd_collection/_view/Cd/all?reduce=false{“error”:”query_parse_error”,”reason”:”Bad URL query key:reduce”}madrugao

  8. Could you please explain the difference between Active record & RelaxDB in detail with some more examples.ThanksRegards

  9. I think CouchDb is fantastically powerful and full of potential. However, I can't wrap my mind past the views and the reduce. However, seeing how RelaxDb uses those views to make some easy to use relationships, properties, and searching for those properties, makes a lot of sense.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s