Nested Forms

Feature is used when you need to save your nested model with the parent model. Use-Case: Suppose you have a model say Person and it can be associated with N number of Contacts. Using nested form we can associate 1..n contacts to the person. Approach: For associating a contact to a person, we need to build the contact model with respect to the person model. For has_many relation, @contact = @person.contacts.build and if has_one relation @contact = @person.build_contact. Now we can use the @contact in the view, for this we use fields_for tag. And

Performance Optimization

In a Ruby On Rails applications where we follow convention over configuration, we might get hit by a slow server response, that would eventually cost users shy away from application. There are many ways of how you can boost performance of Ruby On Rails applications. Focusing on approach depending on the application structure, size of the database and traffic intensity, our performance optimization can be carried out. Rules for Performance Optimization Measure Optimize What is slow Test and Isolate changes Primary areas to think about where we can improve performance are Views and ActiveRecord Queries PS Note : Will be continued.

Migrating (export and import) PostgreSQL database from Heroku to SoftLayer (or dedicated server)

Recently we had requirement in one of the project to migrate Heroku PostgreSQL database to SoftLayer (dedicated server) psql database. One major issue we faced during migration i.e. import Heroku pg database to SoftLayer database using pg_restore is the version conflict between the dump file that Heroku generates (for psql backups) and the old version 8.4. of PostGreSQL that SoftLayer instance has. We upgraded PostgreSQL to version 9.1 on SoftLayer. Remember to upgrade version above Heroku dump file or use same version but not below versions as pg_restore doesn't handle backward compatibility properly for dump

Change URL and enable browser back button with AJAX

We all are aware that clicking any link that sends AJAX call doesn't change the url with the href of the link clicked. As the browsers history is not also updated, back button too will not work. One alternative is to use PJAX that updates the url and browsers history thus enabling back button. What if we don't what to use PJAX? There is way of manipulate browsers history to reflect updated url and enable back button. This technique is used by Github in its file browser. When we click on folder in Github repo, the page updates via AJAX

ZEUS test_environment

Just found that I couldn't do zeus c test for a test environment console Here's a workaround or small hack to do this Steps On your console do zeus init This will create two files zeus.json custom_plan.rb Add this snippet to your custom_plan.rb and define the tasks for Zeus. Mine looks like this: require 'zeus/rails' class CustomPlan Edit zeus.json to include only the tasks for which you’ll use Zeus. Mine looks like this: { "command": "ruby -rubygems -r./custom_plan -eZeus.go", "plan": { "boot": { "default_bundle": { "development_environment": { "prerake": {"rake": []}, "runner": ["r"], "console"

Setting attribute values in active records.

This is an awesome link on how to setup attributes on ActiveRecord LINK: 5-ways to set attributes And I want to mention one trick about active record: When we try to assign a value to active record object like class A In case of any queries, post your queries in comments.

Issue with Unicorn that might occur with multiple requests for same data.

How does Unicorn works out? Unicorn works in cluster wherein it queues requests to multiple workers. Whenever the Unicorn master starts, it loads the app into its memory and handles to serve the requests to its workers. Here is a link which will elaborate the architecture for Unicorn in a nutshell: [Unicorn Architecture(https://github.com/blog/517-unicorn)][1] This is one of the good architecture that helps to distribute requests to provide load balancing. At the same time, there might occur an issue with Data Transaction (Concurrency). Would like to give a small scenario that created a issue in

Log user out of the facebook when user logs out of the application.

As per the facebook policies we need to log the user out of the facebook when he clicks sign\_out in the app. To achieve this there are many ways, some of them got deprecated. One way that is suggested by facebook is to use their [Facebook Javascript SDK (Facebook Javascript sdk)][1] Codes: [Logout of facebook in rails(Logout of facebook in rails)][2] I used following code in document ready to achieve this functionality. window.fbAsyncInit = function() { FB.init({ appId: window.fb_app_id, status: true, cookie: true, xfbml: true, oauth: true }); $('#sign_out').click(function(e)

Devise and integrating Omniauth with Devise

Devise is an authentication solution for Rails. It makes use of Warden which is Rack based authentication framework. Steps to integrate Devise: Include devise gem in your gemfile. gem "devise" Run bundle install to install it. Generally, all the login related details are kept in User model. You are free to use any model name as per your needs. Here User model is used as an example. Generate User model using devise generator command: rails generate devise User Above command will generate user model, migration for user and adds devise routes in _routes.rb_. Now you are all set to

Test Unit Macros.

While writing functional test cases, we might need a common utility to be used through our test cases. These utility is generally recognized as "Macros" and more specifically as "Controller Macros". With Rspec we can easily configure such Macros, as Rspec specifically provide a RSpec.configure block to define all our utility and support classes/modules used throughout our test suit. The RSpec.configure block looks like this. RSpec.configure do config.extend(AuthenticateMacro) end With Test::Unit which is a default testing tool provided by rials and its much more liter than Rspec, we can't