Things which Rails could do but I didn't know.

Run Helper methods in the console $rails c => helper.number_to_currency(100) => "$100.00" => helper.time_ago_in_words(3.days.ago) => "3 days" Shorthand Migrations $ rails g resource person first_name:string last_name:string email:string token:string You can write above command as- $ rails g resource person first_name last_name email token Both will generate same migration: class CreatePeople Add Indexes to migrations $rails g resource person first_name:index last_name email:uniq token class CreatePeople true end end Add Associations to migrations $ rails g resource company person:references name description:text We can

Heroku - How to Setup on local

To Setup Heroku on local machine we first need to add the heroku gem using, gem install heroku then we need to add heroku plugins using, heroku plugins:install git://github.com/ddollar/heroku-accounts.git we then need to add heroku account using, heroku accounts:add project --auto where "project" can be any name that’s meaningful to you, the name is only used locally. We can add multiple heroku accounts using the above command, now we need to set the newly created account using, heroku accounts:set project we can use the above command to switch between

We should use acts_as_paranoid recursively based on requirement.

First of all I would like to inform that [acts_as_paranoid gem][1] is not updated for rails 4. So in our project we have used [paranoia gem][2] which is similar to acts_as_paranoid. Few days ago we faced a issue like once I archived a object then the associated object also deleted because of dependent: :destroy and I was recovering archived object but all details with the associated objects were lost. Example: We had model structure like this class Person acts_as_paranoid has_one :assets, as: :attachable, dependent: :destroy has_many :members, dependent: :destroy has_

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

send email attachments via postmark-api

Previously, I have added post on send email from application via postmark. [Check Here..][1] Now will see how to send attachment with email. In rails, action-mailer provides an easy way to add attachment file as attachments['filename.jpg'] = File.read('/path/to/filename.jpg') Links: [rails-guide-on-attchments(send attachments with email)][2] , [rails-attachments][3] Sending attachments via Postmark-api require "open-uri" def send_refer_pt_request(params, attachment) # attachment -> url of file saved on S3 if attachment.present? mime_type = MIME::Types.type_for(attachment).first url_data = open(attachment).read() attachments["#{attachment.split('/').last}"] = { mime_type:

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.

Send Email with Attachments using Postmark API

From last two days we were running into an issue with the Postmark API based email attachments, specially when you have to use the remote file url for the attachment. Also we were not seeing any error logs for non-delivery of attachments. So Swati and I started with different approaches to solve the problem. I will be explaining here the approach where I directly used the Postmark API key and tried to send an attachment with email using CURL command. Whereas Swati will soon add here post about sending email with attachment using postmark-rails gem and what was causing issue

Hub - create a new github repo!

hub is a command line tool that wraps git in order to extend it with extra features and commands that make working with GitHub easier. Install $ gem install hub $ hub hub standalone > ~/bin/hub && chmod +x ~/bin/hub check successful installation with $hub version Create a new github repo demo-project$ git create [NAME] [-p] [-d DESCRIPTION] [-h HOMEPAGE] annotations -p create a private repository -d set the repository's description -h set the homepage URL This will create a new public GitHub repository from the current git repository and add remote origin at "git@github.com:USER/REPOSITORY.git"

Rails best practices

Please find attached pdf file for Coding Standards. Also, refer following git repo for all Ruby idoms: https://github.com/bbatsov/ruby-style-guide Gem you should consider for refactoring and code formatting: rubocop cane rails_best_practices For code complexity use flog gem. Refer post [here][1] for more details. Happy Refactoring :) [1]: http://knowbuddy.kiprosh-app.com/kyu_entries/ruby-code-quality-metrics-and-security

Capybara-Webkit : How it works and its anomolies

This post is to explain some insights of [capybara-webkit][1] and its functional setup. What is capybara-webkit ? Capybara-webkit is used for acceptance tests in our rails 3 application and it turned out to be non trivial, even though there are excellent tools out there, and they keep getting better. WebKit is an open source web browser engine and capybara-webkit depends on a WebKit implementation from Qt, a cross-platform development toolkit. Using capybara with the capybara-webkit driver, works great because it runs in headless mode, without annoying browser windows popping up. How to setup ? gem "capybara-webkit" Set javascript driver to webkit