Saturday, 10 August 2013

Single Table Inheritance in Ror

Hi Folks/Programmers,
                                      I’ve come across many scenarios ,where a Ruby Rails Developer have to use Inheritance(which is a popular OOPS feature), So today i would like you to share some information about single table inheritance.

 With single table inheritance you have a base model which inherits from ActiveRecord::Base, then one or more sub-classes, which inherit from the base model.

Below here is my simple example:-
My Current example is based on Mobile system. We have a single model called ‘Mobile’ and other models such as ‘Samsung’, ‘Apple’, ‘Nokia’ etc., that inherits the property of Mobile model.

so structure come like

Super class

class Mobile < ActiveRecord::Base


Base class

class Nokia < Mobile


while creating table Mobile  you just create a type string column on your votes table, and rails takes care of the rest..

When you do Nokia.create(model: "E7"), this record will be saved in the mobiles table with type = “Nokia”. You can then fetch this row again using Mobile.where(model: 'E7').first and it will return a Firm object.

If you don’t have a type column defined in your table, single-table inheritance won’t be triggered. In that case, it’ll work just like normal subclasses with no special magic for differentiating between them or reloading the right type with find.

Thursday, 25 July 2013

Viewing associations of a particular model

Today i would like to share about viewing associations of a particular model together in console.
How to get rails associations from console

  1) User Model associate with UserDetail and Photo Model

     class User < ActiveRecord::Base
       has_one :user_detail
       has_many :photos

  2) Open your rails console{ |reflection| ":#{reflection.macro} => :#{}"}

     => [":has_one => :user_detail",":has_many => :photos"]

Monday, 15 April 2013

Ruby Modules && Mixins

include and extend in ruby

Today I would like to explain differences between include  and extend in Ruby.
Include: Include it is adding methods of a class  to an instance which we created.
Extend: Extend it is for adding class methods itself.

If we go with a simple example we can get a clear picture
module Advertisement
def advertisement
puts ‘upgrade to new rails version’

class Movie
include Advertisement
end # upgrade to new rails version
Movie.advertisement # NoMethodError: undefined method ‘advertisement’ for

class Game
extend Advertisement

Game.advertisement # upgrade to new rails version # NoMethodError: undefined method ‘advertisement’ for #<Game:0x1e708>

So that from above example we can get a basic idea that “include” makes the advertisement method available to an instance of a class
and where as the extend makes the advertisement method available to the class itself.

Monday, 8 April 2013

form_for and form_tag

I would like to explain differences between form_for and form_tag

form_tag and form_for both are used to submit the form and it’s elements. The main difference between these two is the way of managing objects related to that particular model is different.


We should use “form_for” tag for a specific model i.e. while crating a new row in database. It performs the “standard http post” which is having fields related to active record (model) objects.

Example for how to use form_for tag:

<% form_for :user, @user, :url => { :action => "update" } do |f| %>

After this, we can use the f as an object to create input filed.

Username: <%= f.text_field :username %>
Email : <%= f.text_field :email %>
Address: <%= f.text_area :address %>

<% end %>


It creates a form as a normal form. form_tag also performs the “standard http post” without any model backed and has normal fields. This is mainly used when specific data need to be submitted via form.


<% form_tag '/articles' do -%>
<%= text_field_tag "article", "firstname" %>
<% end -%>

It just creates a form tag and it is best used for non-model forms.

Wednesday, 27 March 2013

To Include Pagination in application

                                    You can use will_paginate gem to include pagination.

Example :

1) Include following in your gemfile and do 'bundle install'
'gem will_paginate'

2) Use code like below in controller if say your model name is Post

 @posts = Post.where(:published => true).paginate(:page => params[:page], :per_page => 5).order('id DESC')


3) In view file use <%= will_paginate @posts %>           

Monday, 25 March 2013

Difference between .nil?, .blank? and .empty?

1) nil

In Ruby, nil in an object (a single instance of the class NilClass) so methods can be called on it. nil? is a standard method in Ruby that can be called on all objects and returns true for the nil object and false for anything else.

2) empty

 empty? is a standard Ruby method on some objects like Arrays, Hashes and Strings. Its exact behaviour will depend on the specific object, but typically it returns true if the object contains no elements.

3) blank

blank? is not a standard Ruby method but is added to all objects by Rails and returns true for nil, false, empty, or a whitespace string.

Examples :

=> true

=> false

=> false

=> false

=> false

=> false

=> true

=> false

=> true

[1, 2, 3].empty?
=> false

=> NoMethodError

=> true

=> true

=> false

Different Ways for creation of active record object

Rails Allows Programmer to create active record object using following 

1) => "David", :occupation => "Code Artist")
user = do |u| = "David"
  u.occupation = "Code Artist"
3)user = = "David"
user.occupation = "Code Artist"