Featured
-
Installing RVM and Creation of Gemsets Part 2
In the last tutorial we learnt how to install RVM.
by dinesh.singh -
Installing RVM and Creating of Gemsets Part 1
Installing RVM and creating Gemsets can be difficu
by dinesh.singh -
Use pik in Windows
Managing multiple versions of Ruby on Windows can
by anirudh.rautela -
Using Bundler in Rails 2.3.x app
Managing Gems in Rails can be infuriating at times
by anirudh.rautela
Tags
Importing From Excel Files in Rails
Importing excel files in Rails
Hi friends,
Importing excel sheets and storing them into db is a very common usecase of applications. So here I am going to guide you to a step-by-step process of how to import records into db from an excel fi...
Modules
One of the most powerful feature in ruby is module. Modules are like classes but can not instantiated. Module is used for two purpose:
1. namespacing
2. mixin
Namespacing
A class per can be wrapped inside a module to...
Transaction in Rails
To maintain the data integrity sometimes we need the changes to database to occur when all of the actions have successfully finished. One of the examples is funds transfer from one account to another. One account should be debited and anoth...
Rails: Time ago in Words and its Customization
Rails: Customizing Time Ago in Words
Hi friends,
Whenever something is published to a site, we see in their posted time, it is written like about 2 minutes ago or about an hour ago. These kind of sentences displays how much time before the cont...
Rails Scopes
Rails: Scopes
Rails is made for building applications rapidly. Scope is an another example that truly admires this feature. Its use can be explained by taking an example. Suppose you have a blogging site and you want to show everywhere only thos...
Ruby: Dynamic method call using send method
Ruby: Dynamic method call using send method
Hi friends,
As everybody says rails is a dynamic language. They says this because of its functionalities that makes it dynamic. One of the feature of rails is it's send method. It is an instan...
Ruby: Metaprogramming: Method that makes methods
Ruby: Meta Programming: Method that makes Methods
Hi friends,
The normal way of defining method is to give the method name after def and write code between def .... end, and it is pretty fine for most of the cases, but sometimes suppose we ...
Create your own Ruby Gem
Hi friends,
The popularity of Ruby on Rails now a days is so much because of its rapid development, which is possible because of its gems. For every generic functionality there is a gem available, so why not create our own gem. Today I am going ...
Rails Environment Configuration Variables
While developing Rails application we need to keep our project specific sensitive information secure. For example api keys for third parties like stripe, paypal or passwords or configuration data such as the URL. We can set these as environment...
Block, Proc and Lambda
Blocks, Procs, and Lambdas
Hi friends,
Today I am going to explain one of the most confusing part in Ruby, that is Block, Procs and Lambdas. It always troubles you during interviews or any other places wherever you deal with Ruby. But actua...
Integrating payment gateway in Rails - Part 1
Payment gateways are used in websites for online shopping and money transactions. It safely handles customer payments. Payment gateway can easily be integrated in Rails application. Few applications for payment gateway integration in Rails are Sr...
How to deploy Ruby on Rails with Nginx on Ubuntu 14.04 ?
Ruby on Rails runs on web server Webrick. But in order to deploy Ruby on Rails with Nginx server we will need Passenger too.
Install Passenger and Ngnix
First install PGP key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-key...
How to install Ruby on Rails on Ubuntu 14.04 ?
Install Ruby On Rails: Quickest way
The quickest way of installing Ruby on Rails with rvm is to run the following commands:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get....
Rails 5 features
Rails 5 is soon going to release. Many New features have been introduced in this major release. We will discuss the improvements and features here.
1. A few improvements have been made in Turbolinks. Tubrolinks allows us to reload require...
Using friendly_id gem or slugged id in rails
What friendly gem can do?
Friendly_Id is the "Swiss Army bulldozer" of slugging and permalink plugins for Active Record. It lets you create pretty URLs and work with human-friendly strings as if they were numeric ids.
Ra...
Active Admin Gem rails
Installing Active Admin Gem
gem 'activeadmin'
# Integrations possible with:
gem 'devise'
gem 'cancan' # or cancancan
gem 'draper'
gem 'pundit'
Quick Setup Active Admin
...
Text Editor in ruby on rails application
In this article I will guide you to use the tinymce-rails gem to add text editor functionality. TinyMCE is a rich text editor that outputs HTML, makes it easy to add content in rails application.
First you need to add...
Rating Gem in Ruby on Rails
How to setup a rating feature in rails application.
The best and simple way of doing so is by using RAILS GEM ratyrate by following simple steps.
Step 1: Include GEM in your gem file
gem 'ratyrate'
Step...
what is actually module and mixin is?
Modules are similar to classes basically every class object is ancestrally a module object.When you create a class you can create instance of that class through which you can execute class's method but this is not possible in modules as it do...
Setup redis with ruby on rails
Installing Redis
curl -O http://redis.googlecode.com/files/redis-2.2.2.tar.gz
tar xzf redis-2.2.2.tar.gz
cd redis-2.2.2
make
cp src/redis-server src/redis-cli /usr/bin
Redis Server
If you want to use redis-server locally, j...
how to do ajax call through jquery function in rails 4 ?
This is our ruby on rails view from where called our jquery function().
<td id= <%= "reject_#{event.id}" %>>
<%= button_tag "Accept" , :onclick => "abc('#{event.id}', 'Accept');" %>
...
class_eval and instance_eval
What is the use of class_eval and instance_eval?
class_eval and instance_eval allows us to define a method of a class from outside of its original definition and without reopening the class with the standard syntax. This could be useful when...
Difference between include & extend in Ruby Classes
Ruby provides us the facility of Modules. Modules is to ruby as package is to java and namespace is to c#.
We use a module to keep similar kind of classes together, and promote code Re-usability, i.e If a set of code is to be used in multiple ...
Background job using whenever gem rails
Whenever gem is used to easily schedule tasks using cron jobs, it enables you to manipulate standard functionality of your machine using Ruby code.
1: To start with Whenever we have to install gem
gem install whenever # insert this li...
difference between HAS_ONE and BELONGS_TO association in rails?
Has_one and Belongs_to relationship sometime become confusing so the main difference can be explained with the help of example:
1>Has_one :-
class Department < ActiveRecord::Base
has_one: employee
end
this means that employee tab...
Converting a string between camel case & snake case in Rails
There is a method provided in rails to convert a string to the camel case.
The method is camelize.
By default camelize converts a string to upper camel case. If we don't specify any argument in camelize method, it will convert the given s...
how to do adpater settings in rails for postgresql database ?
There are few steps for doing adapter settings:
1> inside your app go to app_name/config/locales/database.yml
default: &default
adapter: postgresql
pool: 5
timeout: 5000
username: postgres
password: 123456
developme...
How to create a new column in an existing table in Rails
This the existing table in migration folder.
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :text
t.timestamps null: false
end
end
end
...
Difference between first & take in Ruby Arrays
Both first & take are applied on the ruby objects, they return the requested number of elements from the Array. There are some differences between them, and i have explained the major ones over here.
1) The method take always accepts an arg...
Difference between && and and in ruby.
Ruby supports both && as well as and.
The basic difference between these 2 lies in their precedence order.
&& has higher preference than and.
and has lower preference than = unlike && which has higher preferen...
Converting string to symbol & symbol to string in rails
Ruby supports both Strings & Symbols.
String being mutable & Symbols being immutable cater to different needs.
We can convert String to Symbol & Symbol to String at any time.
To convert a string to a symbol, we can use inter...
How to remove nil elements from an array in Rails
We can remove nil elements from an array in number of ways. Here i am demonstrating the frequently used methods for removing nil elements from an array.
1> using present method with reject
>> [ "a", nil, "b", nil, "c", nil ].reje...
How to create getter and setter methods in Ruby
Ruby setter & getter methods are similar to properties used in .net. They are used to access the instance variables of a class outside the class using the object of the class. By default instance variables are only accessible inside the insta...
Constructors in Ruby
Ruby also supports constructors like other Object Oriented Languages. Constructors are used to initialize the instance variables. In most of the Object Oriented Languages constructors has the same name as that of the class, and does not have any ...
How to extract today's date & time in Rails
If you want to extract only the date, we can use the strftime method of Time/Date class, as
2.1.5 :055 > DateTime.now.strftime('%d/%m/%Y')
=> "12/30/2015"
(Here d is used to display date, m displays month and Y display the ye...
How to ordinalize current date in rails
Rails provide us many features which are native to Rails and not ruby.
We can ordinalize a date in rails with a method provided by active_support known as ordinalize.
If we want to ordinalize today's date we can write :-
2.1.5 :01 > ...
How to get all month names in the current locale in Rails
There are number of ways to get the names of the all the months in the current locale. I am using the Ruby I18n gem which comes along with Rails.
This gem provides number of methods to perform these localisation tasks.
To get the month ...
How to send a Hash as an argument to a function in Ruby
In Ruby it is possible to send a Hash as an argument to a method.
Suppose we have a method named conversion_method which has 4 default parameters(a,b,c,d). ex:-
2.1.5 :086 > def conversion_method(a:10, b:20, c:30, d:40)
2.1.5 :087?&g...
How to print an object's class & contents in Rails
If i have a User object say user, i.e
>> user = User.find(1)
=> #<User id: 1, login: "james.warner", email: "james.warner@evontech.com",created_at: "2013-11-18 07:12:45", updated_at: "2014-02-03 10:51:02", designation: "Programmer...
How to revert a particular migration in Rails
Rails provides us the concept of migrations. Migrations allows us to alter the schema of our database at any time after it has been created.
It helps user by not letting him/her write the entire SQL query & it uses Ruby DSL(Domain Specific ...
Split array up into n-groups of m size?
A very good method i found in ruby so just thought to share it with everyone, so i am writing this blog to share and help others. This particular blog is for splitting up of array into many groups. Lets see this as an example below:
1) arr = (...
Use .pluck If You Only Need a Subset of Model Attributes
If you want few attributes from a table, instead of instantiating a collection of models and then running a .map over them to get the data you need, its very much economical to use .pluck to pull only the attributes you need as a array.
There...
Spliting resultset over array
I remember i was working on a project where there was a large set of data in the resultset and on the basis of that data i have to update some other model but i was unable to do it in batches due to some reason. So i was searching for a method in...
How to avoid automatically updating rails timestamp fields ?
If you have created_at and updated_at columns in the DB table. Rails will automatically set these values when you create and update a model object. There are chances that we have to update the columns without touching these columns.
You can do...
Batch update in ROR
There are always a requirement when you have to update the multiple records at the same time using ActiveRecord with a single HTTP request. All you have to do is create a hash with keys as the primary key and value as another hash of all the fiel...
How to use Net Beans with Ruby on Rails
Net Beans is an IDE which can be used with many programming languages. We can use it for coding in Ruby on Rails as well.
You can follow the following steps to get started :
1) Download the sh file for the latest version of net-beans.
...
View routes for a particular controller in Rails
In rails it is possible to to view Routes for a particular resource or Controller .
It can be done in the following ways :
1>
rake routes CONTROLLER=name_of_controller
ex:-
rake routes CONTROLLER=users
<2>
...
Sort an array of strings based on length of its elements in Ruby
In ruby if you want to sort an array of strings based on the length of its element, you can use the code given below. i.e If you want the word having the least number of characters in it to come first and arrange other words in this manner as w...
Remove Leading, Trailing and other consecutive white spaces in a string in RoR
There are innumerous ways to remove extra white spaces from a string in Ruby on Rails.
Here i am demonstrating 3 ways which you can use to achieve the desired result.
1> squish function :
This function was introduced in Rails framewor...
Why Should You Consider Ruby on Rails Programming and Its Framework?
Source: tinhthongsoft.com
Ruby, an Object Oriented scripting language for web-development, gets extension from a software library called Rails. As a collective framework for web developm...