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
Form Helpers in Rails Part-4 (Building Complex Forms)
Hi friends,
In my previous blog, we talked about customization of form helpers and how to create forms for external resources through form helpers in Form Helpers in Rails Part-3. Now we will switch to discuss on how to build complex forms in ra...
using head to create header response
While sending an ajax request in rails sometimes we dont need to render any data back to the page . Only status 200 is sufficient to show changes in the view page then we use render :nothing option but there is an alternative to it is head metho...
Use of Render option in Ruby on Rails
render() method in rails is used for rendering a content to the view . Its uses are:
In Controller for rendering an action
Example:
render :action => "show"
This is the most common use of render in controller . The template for sh...
Form Helpers in Rails Part-2 (Model Objects)
Hi Friends,
In my previous blog Form Helpers in Rails Part-1, we have talked about only the basic form helpers. Today we will be discussing forms with models. As we know in MVC, forms are mostly used for creating, updating or editing models. So...
Form Helpers in Rails Part 1 (Basics)
Hi Friends,
In my previous blogs, I have talked about so many topics related to Ruby on Rails. Now today I am going to start form helpers in rails. As all the points can't be covered in one blog, I am dividing it into parts. Today I will be deal...
Applying partials to layouts in rails
In Rails we can use partials to organize our code in the layouts . This is generally helpful when we have layouts running up to hundreds of lines. For example we have a top nav bar which looks different for logged in and non logged in user. Then ...
Use of :as in Passing parameters to rails partials
While passing objects to partial we can change the name of the local variable in the partial and for that purpose we need to use the :as option.
For example we have a partial _account.html.erb where we need to pass a collection @cutomers a...
Capturing part of template in variable in RoR
Hi Friends,
Sometimes you need to use a small part of a template at so many places and you have to write the same lines of code on separate places and you also don't want to create separate partials for that. Rails gives a solution to your probl...
Number Formatting in RoR Views
Number Formatting in Rails
Numbers are of different formats, according to their uses like phone numbers, percentage, currency etc. In Rails NumberHelper fulfills this requirement by providing methods for formatting numbers into different forms. ...
Passing parameters to rails partials
While rendering a partial if we want to send parameters to the partial we can do it like this:
<%= render :partial => user_profile_list, :locals => { :user => @user } %>
or
<%= render :partial => "user_profile_list", :...
Benchmark helper in rails
Rails has benchmark helper to quickly test the time of execution of a given piece of code. Active Record, Action Controller and Action View libraries provides a benchmark() method to measure the performance of code.
Example:
Model(Active Rec...
Atom Feeds in Rails
Hi friends,
We have recently talked about auto_discovery_link_tag, that consumes RSS and Atom feeds. Now we are going into deep what is the atom feeds in Rails and how we can achieve that. Lets begin with a defining Atom Feeds.
Atom Feeds are ...
auto_discovery_link_tag in rails
Hi guys,
Today we are going to discuss about auto_discovery_link_tag in rails. It comes under AssetTagHelper, which gives methods for generating HTML that links views to assets(i.e images, JavaScripts, stylesheets, and feeds).
For consuming RS...
Builder templates in ROR and its uses
Action view templates can be written in various ways depending on the format of the response. If the response is in html format then we can use .erb files i.e. html files, with embedded ruby in it.
If the format is in xml then we need to use fil...
Introspection using ActiveModel
ActiveModel is a library which contains various modules which when added to a class add some features present on Active Record to the class. One such module is ActiveModel::Naming module. This module is used for model name introspection .Below ...
Dirty methods in Rails model
Sometimes we face a situation where after changing the value of an attribute of an object we need the previous or the unchanged value for some purpose. We generally store the previous value in a variable to be used later. Instead of this approach...
Rails Model Translation
Rails Model Translation
Translation in Rails active records is used to provide integration between object and the Rails internationalization (i18n) framework. Rails does this using ActiveModel::Translation.Example:
class Blog
ext...
Rails model Serialization
Rails model Serialization
Hi Friends,
Earlier we have discussed about Association in Rails Model. Today we will be covering one more topic related to rails active records, that is Serialization. In summary we can define serialized objects as, S...
Join in rails model
In rails if we want to establish inner join relationship between two models we can do it using joins method of activerecord.
For example, consider the following Company, Product, Review and Vote models:
class Company < ActiveRecord::Base...
Associations in Rails Model
Associations in Rails
Associations are an important part of each Active Records. It makes coding easier and relations clean. Lets take an example of Blog. A Blog can have many comments. So if you delete blog its associated comments also needs ...
Reorder in Rails
Hi friends,
Today we are going to discuss about reorder in rails. This method is used to override the scope of order and reorder the rows according to the passed arguments.
Example:
Blog.order('title DESC').reorder('published_at') # generate...
Having Clause in Rails
Hi Friends,
Previously we discussed about Group By Clause in Rails, Now today lets discuss a related clause of SQL, which is Having. SQL uses having for specifying conditions on GROUP BY clause. Suppose we want to fetch the total views of blogs ...
GROUP BY clause in Rails
Hi Friends,
As you know GROUP BY is used to group the result-set by one or more columns. The SQL query to fetch the total views of blogs created at same day will be:
SELECT date(created_at) as creation_date, sum(view) as total_views
FROM blo...
Self Join in Rails
In Self join association one model has relation with itself. For example one user table can have list of all coach and player. One coach can train many players. We will represent the association as:
class User < ActiveRecord::Base
has_m...
Fetching records in batches in rails
Hi Friends,
Many times we get into a situation where we have to fetch a large record from a table and need to iterate over it and that makes our server cry. As it consumes a lots a memory and time. So no need to worry again for that Rails has a...
Polymorphic association
Polymorphic association is little tricky to understand among all the rails association. Using this association one model can have belongs_to association with more than one model in a single line of association. As for example in a social networki...
Fetching Records in Rails using take
take in Rails:Rails has so many methods that provide the facility to fetching the records from database, so that you don't need to write raw sql. One of them is take.
By using take we can fetch a record (or N number of records if specified as a ...
find_by in Rails
Hi Friends,
Rails provides several methods for fetching single object from table, one of the method is find_by. It fetches the first record in the table matching some specific conditions. Example can be if we want to fetch the first blog with ti...
Choosing Between has_many :through and has_and_belongs_to_many
In Rails we can establish many to many relationship between two models in two ways 1) by using has_and_belongs_to_many 2) by using has_many :through associations.
We generally use HABTM when we dont require a third intervening model to join t...
Unique Validators in Rails
Hi Friends,
Today we will be going to discuss a very basic constraint of rails validation that is uniqueness, as its name suggests, Unique Validator is a rails validation helper used to check an attribute value in database and if it already exis...
Strict Validator in Rails Model
Hi friends,
We already talked about different kinds of validations. Today lets discuss one of the common type of validation that is Strict validation. By using this we can check whether an object is valid or not and can raise ActiveModel::Strict...
Numeric Validator in Rails
Hi Friends,
Thanks for viewing my previous blogs, hope you liked them. As we were previously talking about validations starting with Format validations in Rails, lets continue with that with a new validation helper numericality. As its name rep...
Length Validators in Rails
Hi Guys,
In my previous blog Format validators in rails, I talked about validations in rails and now I am here again with one of the most commonly used helper available in rails, that is length.
Length validator is used to validate the length ...
Format Validators in Rails
Hi Friends,
Hope you are doing well with your work. Today I am here with one of the most common topics in Rails that is format validators in rails. Lets start with defining validation.
"Validations are used to avoid the invalid data to get sto...
Choosing Between belongs_to and has_one
If we have has_one relation between two models then we need to declare belongs_to relation in one model and has_one relation in another model. belongs_to will be declared in the model where we will have the foreign_key and has_one in the other mo...
Virtual attributes in Rails like acceptance and confirmation
Hi Friends,
We have talked about few Rails topics earlier like validates associated and overriding naming convention of table names. Here I am again with one more topic Virtual Attributes in Rails. Before discussing that lets look into a situati...
Pessimistic locking in ruby on rails
In pessimistic locking a particular row in the database is exclusively locked so that other updates on the same record are blocked . This mechanism has the advantage that when a certain operation is going on the selected row other user cannot rea...
Optimistic locking in rails
Optimistic locking is used to restrict updation of a record by multiple user at the same time . For example we have two user u1 and u2 and both of them is trying to edit the same record from a model City . While u1 is editing the record u2 c...
Validating Associated models in rails
Hi Friends,
I am here again with one more topic in rails that is validates_associated.
So as we all know that associations are a major part in every kind of Relation Databases. So what happens if we have one model with some associated objects ...
Overriding Name Conventions for table name and primary key in rails models
Hi friends,
Today we are going to talk about overriding the default naming conventions for table name and primary key in rails model. Before switching directly to the topic lets take a look to the summary of default naming conventions for dat...
has_and_belongs_to_many relation in rails
has_and_belongs_to_many association is used in rails where we dont need a third joining model to establish the relation between the two model. There will be no additional columns (except the foreign keys) of the third joining table.
For exampl...
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 hash and hashwithindifferent Acess
The Hash class in Rubys core library retrieves values by doing a standard == comparison on the keys. This means that a value stored for a Symbol key (e.g. :my_value) cannot be retrieved using the equivalent String (e.g. my_value). On the other ha...
How to catch phone number in string
While sending internal message on a system people usally share there phone number and making call outside the system which divert traffic from our system. So while exchanging messages within system we need to hide the phone number. This can be ac...
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...
Top 5 ruby gems most helpful in application
Devise(https://github.com/plataformatec/devise): It represents authentication mechanism. Very powerful, flexible and also it allows to integrate Outh authentication system with minimal effort.
FriendlyId(https://github.com/norman/friendly_id...
Configuring i18N Api in rails internationalization
Rails Internationalization : Configure the i18n Module
As we all aware that whenever we use term web, it refers to globalization of application. For globalization, your application must support multi-language support. In ruby i18n provides t...
Encoding issues while parsing CSV file using Roo gem in rails
This is the most common and frustrating issue while parsing a csv file in rails i.e. the encoding issue. So in ruby on rails i found a library which basically detects the Character encoding using ICU (dependency package) and the name of the libra...
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...
Collect and Map In Ruby On Rails
Map:
Map use to take the object of enumerable and returns a new array with the results of running block once for every element in enum, the block is something like [1,2,3].map { |a| a+2 }
and the outcome of [1,2,3].map { |a| a+2 } will be [3...