Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 

Ordering, Limit and Offset in Active Records Rails

Active records provide us with ways to set constraints in our queries to specify the order in which we have to get the records from the table. This can be done through the following ways:  order This method is used to get recor...

Retrieving a Single Object From Active Records In Rails

Active records gives us different ways to retrieve single objects from it. Those methods with examples are given below :  find The find method allows us to retrieve a single object or we can say single record from the database ...

Active Record Migrations In Rails

Migrations in Rails Active records provide us with a very useful feature to change or alter our database schema in ruby on rails. Through these migrations we can make necessary changes to our database and make changes in our tables and rows...

CRUD operations in Rails

CRUD Operations in Rails Before understanding CRUD operations in rails we will see what are active records. Active records point to the models of MVC architecture. Model is the component of mvc architecture which is responsible for persisting ...

Implementing file upload functionality with PapaerClip Gem

Uploading a file in rails through Paperclip As we know these days almost all the applications have the requirement to upload files to the application then be it an image , a video or any other file. I am writing this blog to share with you all...

Writing Skinny Controller In Rails

Hi friends today I am going to guide you how you can make our controller skinny by following some basic object oriented approach in rails. Often we start writing our program business logic in our controller thus making mess of our controller as t...

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 ...

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 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 ...

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> ...

How to install Rails in a particular gemset for a particular version of Ruby

1> For this we should first confirm the ruby version we are currently on rvm current (ruby-2.2.3) 2> To view the list of available gemsets before selecting a particular gemset, we can type rvm gemset list gemsets for...

SETTING UP ROR FOR THE FIRST TIME ON A NEW UBUNTU SYSTEM USING RVM

1> We need to install RVM before we can install Ruby, because RVM is a version manager and it will help us to install & manage different versions of Ruby on the same system and easily switch between them. To install RVM we first need to...

each, collect and map in ruby

1> 'each' will loop through each element of the array and evaluate whatever is return inside the block, but it returns the original array without any change. a = [5,10,15,20] => [5, 10, 15, 20] a.each {|t| t+2} => [5, 10, 15, 20...

Using reject in ruby to delete elements selectively from an array

1> To delete an element from an array, use reject as shown below: arr = ['a','b','c','d','e'] arr.reject! { |i| i.match('d')} Result :=> ["a", "b", "c", "e"] 2> To delete particular keys from the array whose elements are ...

How to get date with suffix st,th etc

While getting date in rails we do have a requirement where we need to show suffix attached to date like 2nd,3rd etc. There is no direct rails date function to get the same so in order to get date in the following format Thu, Nov 5th 2015 ...

Hiding all the URL in a text except a particular one

There was a requirement of hiding all the wesite URL in a message except for a particular one. While looking for a solution I came across a particular solution which I think might help other . params[:meal_note] = params[:meal_note].gsub(/...

Applying limit and offset to array

In rails we can apply limit and offset to arrays in different ways. 1> Using "slice" method. slice takes 2 arguments, the first one being the "starting_index" & the second one is the "number_of_elements". syntax :- arrayob...

Configuring Unicorn for rails app

Unicorn is an HTTP server for Ruby. It is designed to be used in production environment unlike Webrick which is designed to be used at the time of development on local machine. Because it is very fast, efficient and offers lots of good features l...

Explain Validation helpers

Active Record gives many predefined validation helpers which you can use directly in your class. These helpers provide us rules which can be commonly used as a validation rules. So if the validation fails, a error message is added to object error...

Understand Rails Authenticity Token

What happens when the user views a form in the browser for a resource, rails application creates a random string as a authenticity token and store that random token in the session and when the form generates, it places that token in the form as a...

How to check variable is being passed in partial

While working in rails we make use of partials very often and there are case where we make use of same partial for different scenario. We try to achieve the same by passing default parameter for some unwanted variable which might case issue. Pass...

Difference between nil?, empty? and blank?

Difference Between nil?, empty? and blank? in Rails :- To understand the difference between these three methods you have to look at each of them individually. In Ruby nil? is a standard method that can be called on all objects and returns...

Managing Rails Versions and Gems- Automatically load required version using RVM

If you are using rvm and want to load project specific ruby version and gems automatically when you access the project directory, for this you can use a very simple command rvm --create --ruby-version use ruby-version-number@gemsetname ex...

Group By With Where Or Having clause in Rails

Group By : As we all know that group by in any RDBMS provides us with a way to get aggregate of rows based on any particular column. Taking it in point of rails the enumerable class in Rails contains a method named 'group_by'. This method is a pu...

How to Apply Active Record Observer on a Particular Column

Meaning:- Active Record Observers means a callback or a trigger which will get called in the life cycle of active record object. This is used for the purpose of reducing the burden on model's functionality which is not directly belongs to model....

Parition of log files

Generally we got errors at the time of development so logs are the best place to find out the errors. Its easy to deal with log file on our local machine but if there is a live site and we have to check out for logs and logs are getting larger so...

Install pdfkit and wkhtmltopdf library on Ubuntu 12.04/ruby

This library is used to create pdf from html. Steps to install: #Install wkhtmltopdf library in ruby by using below command: apt-get install wkhtmltopdf #Use below command to find the path. This path need to add in in pdfkit.rb[in i...
prev 1 2 next
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: