Tuesday, November 11, 2014

Rails Active model serializers

When to use active model serializer ?

    When you have your app running on both browser and mobile app, and if you have a backend rails server then active model serialiser is a perfect gem for serving api’s.

There are other gems like rabl but I personally prefer this gem because this is extremely easy to use and it has lot of + points.


Step 1.

    Add gem into your Gemfile

    gem ‘active_model_serializers'


Step 2.

    Create a serializer file by running

    rails g serializer user


    which will create  app/serializers/user_serializer.rb



Step 3.

    In serializer, you can add attributes, associations, custom methods.

    attributes :id, :name, :followers, :timestamp


So here id and name are user fields. Followers is an association in user. timestamp is a custom method.

You can define that method like this.

    def timestamp
        object.created_at.strftime(“%B %d %Y”)
    end       

Step 4.

    In your controller, you probably would have added

        respond_to :json, :html 
    and in action

        respond_with @user

When you visit /users/1.json
   
{

    "user": {
        "id": 1,
        "email": "foodoo@example.com",
        "name": "Foo doo",
        "microposts": [
            {
                "id": 1,
                "content": "This is a micropost",
                "user_id": 1,
                "created_at": "2014-11-11T16:11:08.500Z",
                "updated_at": "2014-11-11T16:11:08.500Z"
            }
        ],
        "followers": [ ]
    }

}

Saturday, April 26, 2014

Multiple solr's in single solr instance

Step 1:

Go to your solr directory.
cd /opt/solr141/
sudo mkdir home1
copy contents into home1 directory from other home directory.
sudo cp -a /opt/justbooks/solr141/home/. /opt/justbooks/solr141/home1

Step 2:

create solr1.xml under tomcat6/conf/catalina/localhost. (<your url>/solr1) Copy from solr.xml and just change value to point to new home dir in environment tag
 
<Context docBase="/opt/solr/solr141/dist/apache-solr-1.4.1.war" debug="0" crossContext="true" > <Environment name="solr/home" type="java.lang.String" value="/opt/solr/solr141/home1" override="true" /> </Context>

Step 3:

In
/opt/solr/solr141/home1/conf/Solr-config.xml 
<dataDir>${solr.data.dir:/opt/solr/solr141/home2/data}</dataDir> <locktype>simple</locktype> <unlockonstartup>true</unlockonstartup>

Step 4:

Restart tomcat

Step 5:

Since we copied from other home folder, we need to clean up the index
curl localhost:7585/solr3/update --data '*:*' -H 'Content-type:text/xml; charset=utf-8'

Friday, March 14, 2014

Fetch records from sql query - Ruby On Rails - Oci8 cursor

Let's say we need to fetch the records from this sql query in rails.
  
s = ActiveRecord::Base.connection.execute("select id, email from users" )

The output will be something like
  
  < OCI8::Cursor:0xf73ac30 >

which is nothing but a cursor object. But how to read the records from this cursor?.
The code to fetch the record is
  
while r = s.fetch_hash
   puts s
end
The output will be
  
{"ID"=>10005, "EMAIL"=>"2@gmail.com"}
{"ID"=>10006, "EMAIL"=>"3@gmail.com"}
{"ID"=>10002, "EMAIL"=>"1@gmail.com"}