Retrieve image dimensions

There are various ways to retrieve when using Carrierwave. They are as follows: Using RMagick gem: class ImageUploader Using ImageMagick gem: class ImageUploader These are pretty good ways to retrieve image dimensions on server-side. It would be much more easier if we could handle it on client-side (JQuery). There could be many ways to do so. Below is the way I adopted in one of the project using FileReader API. $("input[type='file']").live 'change', (e) -> file = this.files[0] reader = new FileReader() img = new Image() reader.onload = (e) -> img.src = e.target.result img.onload = (e) ->

ActiveRecord::Dirty

Performing an action via a callback is fairly easy and doable. Sometimes requirement may be a bit enhance that we need to track every change now and then. On the same note, if we have associations and we wanted to make some changes that needs to be reflected on previous and new values, this dirty concepts can save our time. This module gives a way to track changes done in an active-record object. By default, rails include ActiveRecord::Dirty this module. This gives many powerful methods that helps us to track attribute changes. This methods are based on the attribute

Lessons learnt and Good Practices

There are several practices followed in Rails. Since ruby is the backbone of rails, here are few practices that are recommended: Lets consider a class ABC: class Animal attr_accessor :id, :type def self.mouse(object, a1, a2) puts "#{self}" end def animal puts "I m in animal instance method" yield if block_given? end def id @id.to_i end def initialize(id, type) @id = id @type = type end def update_extra_attrs(a1, a2) puts "#{self.inspect}" end end Execution & Explanation: To change the behaviour of attribute's value, always make use of ruby's getter and setter methods.

Query String in rails

Most of the times we look up to pass extra params in url. This post will give an overview of possible query string formation but in rails way. For this, we know the common syntax for query string is "/controller/action?name='A'&id=1", where multiple params are separated by '&' literal. Here are the common rails ways: to_param => Returns a String, which Action Pack uses for constructing an URL to this object. i) When operated over hash, it will assignment string. For say, => {a: 1}.to_param => "a=1" ii) When

Pjax in rails

For any applications, we require many jquery and css files to be loaded on each request (i.e HTML requests). Instead, we can render all the files at once and reuse these files over-and-over again to save our page-load time. To serve this purpose, we have several gems and one of them is rack-pjax. rack-pjax uses ajax and pushState to deliver a fast browsing experience with real permalinks, page titles, and a working back button. pjax works by grabbing html from your server via ajax and replacing the content of a container on your page with the ajax'd html. It

after_commit callback in test environment

The plus point of having test environment is that we can test our whole application in a different dedicated environment without any manual efforts. Technically, it involves views, controller methods, request/response, routes, models(callbacks, validations, etc.) There are few scenarios in which we'd require to test after_commit changes.The application which manipulates data majorly in after_commit callback could cause issues in test environment thus causing it to fail. The problem is that the after_commit callback is never fired in rspec if we use transactional fixtures. -> What is transactional_fixtures ? -> ActiveRecord’s fixtures,

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

Mass Assignment in Rails 3

What is Mass Assignment? Assigning multiple attributes of a record with params received from the application within a single query. For example,{:user => {:first_name => "FirstName", :last_name => "LastName", :role => "admin"}} So while creating a new record in User table, we specify as => User.create(params[:user]) In the above query, rails gives the freedom for one-to-one mapping of params attributes with model-record attributes. Lets say, we don't explicitly specify as User.create(:first_name => params[:user][:first_name], :last_name => params[:user][:last_name].......). Such assignments