Posts

Showing posts with the label Sabyasachi Ghosh

Swap primary keys between two records in the same table in Postgres

What if we need to swap two primary keys on the same table? What's the best and efficient way to do the same? let's check Now:- Let's assume we have a user table which has 3 columns among all the rows I have taken two sample rows which are the target rows for us. Id Name Email 101 Sabyasachi XX@gmail.com 105 Saghosh xxx@gmail.com We need to swap the primary keys (which are unique by nature) between those two records so our desired result will be something like   Id Name Email 105 Sabyasachi XX@gmail.com 101 Saghosh xxx@gmail.com Now lets buield a query using CTE(common table expression) to solve the problem:- with source_user as (   select     id   from     users   where     id = 101 ), destination_user as (   select     id   from     users   where     id

Postgres database is not running after OSX crash

Every one might have face the problem that Postgres id not running after OSX is crashed. Every time you try to start the Postgres server it will say : - another server might be running; trying to start server anyway You can follow the below steps to get rid of the problem. Step 1: Open your terminal and run   pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start   It will Say   pg_ctl: another server might be running; trying to start server anyway Step 2:  Check the log $ tail -f /usr/local/var/postgres/server.log It will print something like this FATAL:  lock file "postmaster.pid" already exists HINT:  Is another postmaster (PID 326) running in data directory "/usr/local/var/postgres"? FATAL:  lock file "postmaster.pid" already exists HINT:  Is another postmaster (PID 326) running in data directory "/usr/local/var/postgres"? Step 3: Delete the PID file. $ rm /usr/local/var/postgres/postmaster.pid S

Create Nested form In Ruby on Rails 4.1

In Rails we create simple form and also sometimes we need nested form. In Rails we can easily do that using  gem “nested_form”. let's create a project using “nested_form”. Before that I would like to ask you to have a look on nested_attribute_documentation(http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for). 1) Create a new Rails 4 Project.     rails new nested_app 2) Include the gem in your gem file.   gem "nested_form" 3) run bundle install  bundle install 4) Generate the necessary javascript file.   rails g nested_form:install 5) Include the javascript file in the asset pipeline. in the application.js add the below line.   //= require jquery_nested_form 6) Let say we have a Exam model and one Exam can have multiple Questions.So lets create the Exam model. class Exam < ActiveRecord::Base   has_many :questions, dependent: :destroy   accepts_nested_attributes_for :questions,

Creating RESTful API using Grape and RABL in Rails 4

We can create a RESTful API using Grape and RABL in Rails4. Step 1: Add this GEMS in your Gem file, And do Bundle install.  gem 'grape'  gem 'rabl-rails'  gem 'grape-rabl'  gem 'rack-cors', :require => 'rack/cors' Step 2: Create a file under lib directory called api.rb, and create a folder inside the view named as api. # lib/api/v1/root.rb class API < Grape::API end Step 3: Add this line to your application.rb file    config.paths.add "app/api", glob: "**/*.rb"     config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/api/*)     config.middleware.use Rack::Cors do       allow do         origins '*'         # location of your API         resource '/*', :headers => :any, :methods => [:get, :post, :options, :put, :delete]       end     end     config.middleware.use(Rack::Config) do |env|       env['api.tilt.root'] = Rails.root.join "app", "

Set up ruby 2.0 and Rails 4.0.1 in Rackspace

First we need to install ruby Step 1)Install Ruby apt-get -y update apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev cd /tmp wget http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz tar -xvzf ruby-2.0.0-p247.tar.gz cd ruby-2.0.0-p247/ ./configure --prefix=/usr/local make make install Step 2) Install Rails gem install rails 4.0.1 Step 3) Install passenger gem install passenger Step 4) Install Apache2 apt-get install apache2 Step 5) Install Mysql sudo apt-get install mysql-server mysql-client That's it we are done with the setup.

Active resource in rails

This is a powerful feature of rails to communicate between two models in two different application running in two different server. ActiveResource:Base is the main class for mapping RESTful resources as models in a Rails application. Active Resource objects represent your RESTful resources as manipulatable Ruby objects. To map resources to Ruby objects, Active Resource  only needs a class name that corresponds to the resource name (e.g., the class User maps to the resources Products, very similarly to Active Record) and a site value, which holds the URI of the resources. class User < ActiveResource::Base   self.site = "http://api.products.com:3000/" end In order to access products.com controller you need to send the json response like i am going to call the index action so we need to write :- def index @products = Product.all respond_to do |format| format.json { render :json => @products} end end Now the User class is mapped to RESTful resources located