Getting start with Rails3 and Couchrest_model


It’s not difficult to use Couchdb with Rails3. Most of it comes down to making sure that you’re not loading ActiveRecord and understanding how to manager the new Rails dependency.

Installing Couchdb

1. For couchdb, We need install  g++, erlang  and few adapter, so that couchdb will work smoothly.

Run below command form terminal,

$ sudo apt-get install g++
$ sudo apt-get install erlang-base erlang-dev erlang-eunit erlang-nox
$ sudo apt-get install libmozjs185-dev libicu-dev libcurl4-gnutls-dev libtool 

2. One we done with above installation, go to couchdb site and download coach db source file.

In a terminal, go to the folder where you have downloaded the file, extract and go to bin folder and run below commands.

$ ./configure
$ makee
$ sudo make install

That’s it, you done with coachdb installation.

Creating a new Rails Application With couchrest_model

The important thing here is to avoid loading ActiveRecord. So we will use --skip-active-record to skip ActiveRecord initialization.

$rails new cast --skip-active-record

The first thing we need to do is to add the Couchrest_model gem to the Gemfile.

source ‘https://rubygems.org‘ application
gem ‘rails’, ‘3.2.13’
gem ‘couchrest_model’
group :assets do
gem ‘sass-rails’,   ‘~> 3.2.3’
gem ‘coffee-rails’, ‘~> 3.2.1’
gem ‘therubyracer’, :platforms => :ruby
gem ‘uglifier’, ‘>= 1.0.3’
end
gem ‘jquery-rails’

Bundling

Once you’ve configured yeeeour Gemfile, We can then install the gems running the bundle installer:

$ bundle install

Installing couchrest (1.1.3)
Installing couchrest_model (1.1.2)
Using jquery-rails (3.0.4)
Using rails (3.2.13)
Using sass (3.2.9)
Using sass-rails (3.2.6)
Using uglifier (2.1.2)
Your bundle is updated!

Configuring

Once the gems have installed we’ll need to run the Couchdbrest_model configuration generator so that it can create the configuration YAML file.

$rails generate couchrest_model:config

The default file is shown below. We can leave it as it is while we’re developing our application.

config/couchdb.yml

development: &development
   protocol: 'http'
   host: localhost
   port: 5984
   prefix: cast
   suffix: development
   username:
   password:
 test:
   <<: *development
   suffix: test
 production:
   protocol: 'https'
   host: localhost
   port: 5984
   prefix: cast
   suffix: production
   username: root
   password: 123

Everything is in place now for us to begin building our application. We’ll start by creating an User model with email, first_name and last_name fields and use Rails’ scaffolding to create the associated controller and view code.

$rails g scaffold user email:string fname:string lname:string

If we open up the model file we’ll see that it’s a simple class that inherit  CouchRest::Model::Base

class User < CouchRest::Model::Base
 end

you can now start to add properties to the model.

To define a property, simply call the property class method in your model definition:

class User < CouchRest::Model::Base
     property :email, String
     property :fname, String
    property :lname, String
 end

Our application is now ready to run. We don’t need to run any database migrations as CouchDB is schema-less. Now it’s time to run server. Make sure your Couchdb(sudo couchdb) is running in other terminal OR background.

Advertisements

Rails couchdb demo application


I was trying to create a demo application using couchdb as backend. I faced to many challenges to create demo application.

– couchdb installation on ubuntu 12.10

– Deciding  ORM

– Create demo_app

– configure couchdb.yml file

– Initialize model attributes.

– Access couchdb data from UI interface.

Below are the step by step instruction to fix all above issues and create a demo application.

1.  couchdb installation on ubuntu 12.10

I already blogged Couchdb installation instruction from source code. followed all steps to install couchdb on your ubuntu machine.

2. ORM

I used ‘couchrest_model’ ORM. Install couchrest_model using as ruby gem.

$ sudo gem install couchrest_model

3. Create demo application

$ rails new rails_couchdb –skip-active-record

Add couchrest_model gem in your gemfile.

gem 'couchrest_model'

Do bundle update.

4. configure couchdb.yml file

$ rails generate couchrest_model:config

then create order scaffold.

$rails g scaffold order name:string details:text phone:integer email:string

5. Initialize model attributes.

You need to require couchrest_model in your rails order model.

require 'couchrest_model' 

and add field using properties keyword.

  property :name,      String
  property :details,      String
  property :phone,     Integer, :default => 95556236
  property :email,      String

Now start your rails server, you able to perform CRUD operation on order table.

6. Access couchdb data from UI interface.

Visit http://127.0.0.1:5984/_utils/  to access Couchdb data.

That’s it.

Enjoy….!!!!!!!!

 

 

install couchdb on ubuntu


1. For couchdb, We need install  g++, erlang  and few adapter, so that couchdb will work smoothly.

Run below command form terminal,

$ sudo apt-get install g++

$ sudo apt-get install erlang-base erlang-dev erlang-eunit erlang-nox

$ sudo apt-get install libmozjs185-dev libicu-dev libcurl4-gnutls-dev libtool 

2. One we done with above installation, go to couchdb site and download coach db source file.

In a terminal, go to the folder where you have downloaded the file, extract and go to bin folder and run below commands.

$ ./configure
$ make
$ sudo make install

That's it, you done with coachdb installation. 

3.Next we need to start coachdb.
$ sudo couchdb
Apache CouchDB 1.2.0 (LogLevel=info) is starting.
Apache CouchDB has started. Time to relax.
[info] [<0.32.0>] Apache CouchDB has started on http://127.0.0.1:5984/

4. Setting CouchDB as service
Run below command, that will set couchdb as service, When ever you start ubuntu.Couchdb will run as service.

$ sudo adduser --disabled-login --disabled-password --no-create-home couchdb
Adding user `couchdb' ...
Adding new group `couchdb' (1001) ...
Adding new user `couchdb' (1001) with group `couchdb' ...
Not creating home directory `/home/couchdb'.
Changing the user information for couchdb
Enter the new value, or press ENTER for the default
 Full Name []: CouchDB Admin
 Room Number []:
 Work Phone []:
 Home Phone []:
 Other []:
Is the information correct? [Y/n] Y

That's it.

Cheers.



Couch DB


CouchDB is a NoSQL, schema-free JSON document storage where JavaScript can access the database directly via HTTP with no middleware required!

1.JavaScript can access the database directly.

2.Browser-based GUI-> Play with your data, permissions and configuration right from the browser with CouchDB’s interface Futon.

3. Replication->Replication synchronizes two copies of the same database, allowing users to have low latency access to data no matter where they are. These databases can live on the same server or on two different servers—CouchDB doesn’t make a distinction. If you change one copy of the database, replication will send these changes to the other copy.

4.Security-> It also has database-level security. The permissions per database are separated into readers and admins. Readers can both read and write to the database but admins can also update the _design documents in a database.

5.validation-> The ability to validate inserts is built right into the database. You can combine with authentication to check to make sure the creator of the document is the one who is logged in.

6.Map/Reduce, List and Show-> You can take a bunch of documents, emit what you need and then reduce it down based on grouping relevant data.

Cheers.