Tuesday 16 December 2014

Devise Confirmation - Quick tip :

We all are mainly using Devise Library for user authentication purpose, as you may experienced a scenario where user has to confirm account by mail confirmation link. It was a real burden for me :) as i had to set up Smtp Gmail settings in my local development environment, To avoid this i used a quick tip by updating particular user confirmed_at attribute. Code snippet for it is as follows.

User.last.update_attributes(:confirmed_at => Time.now)



Wednesday 3 September 2014

Postgresql with Hstore in Ruby on Rails 4 applications :)

We can use Nosql in Postgres :-), Excited right ? Yep Ruby on Rails 4 supports this via Hstore.

I had to query my Model tuples but it was not possible as i was using Serialize to store dynamic fields. But using Hstore we can easily achieve this like as follows , Hstore acts same as serialize in rails, but additionally we can query tables using Hstore.

 console
   p = Chocolate.last
   p.properties = {'color' => 'orange'}
   p.properties['price'] = '5'
   p.save
   Chocolate.where("color -> 'orange' ")
   Chocolate.where("price -> '5' ")
Follow steps below to enable Hstore in Rails 4 + Postgresql applications.
   rails generate migration enable_hstore_extension
   class EnableHstoreExtension < ActiveRecord::Migration
     def change
      enable_extension 'hstore'
     end
   end
   rake db:migrate
Now create a migration that will add a column called 'properties' to our Chocolate model.
  rails g migration AddPropertiesToChocolate

  class AddPropertiesToChocolate < ActiveRecord::Migration
    def change
      add_column :chocolates, :properties, :hstore
    end
  end
  
  rake db:migrate

Thursday 5 June 2014

Bang Methods in Ruby Language

Bang Methods in Ruby Language :

             Hi today i would like to share some information regarding Bang methods in Ruby. Please take a look at following examples and give a try in rails console.

 name = "JOHN" 
 p string = name.downcase # prints john, doesn't modify name 
 p name # name is still JOHN          

Method which ends with exclamation mark in ruby is called as bang methods.
         
name = "JOHN" 
p name.downcase! # prints john,modify name 
p name # name becomes john after calling bang method

Wednesday 14 May 2014

Difference between && , and in Ruby

Difference between && , and in Ruby is mainly the Precedence of these operators. ‘and’ and ‘&&’ both have different precedence. This tutorial will help understanding with the help of examples.

Use of ‘&&’

The ‘&&' operator is used as "Logical AND” on the boolean/ non boolean variable in Ruby.
Example,
Suppose we have variable a as,
a = true
And variable b as,
b = false
Then, logical AND will give result as,
c = a && b
puts "C is #{c}"
=> C is false

Use of ‘and’

The 'and' operator is also used as "Logical AND” on the boolean/ non boolean variable in Ruby.
But, ‘and’ has lower precedence than ‘&&’
This will also give same results for example shown for ‘&&’
a = true
b = false
c = a and b
puts "C is #{c}"
=> C is false

Difference

Suppose, we have a, b and c variables as given below,
a = true
b = false
c = true

Use of && - Use of and

d = a && b && c
puts "D is #{d}"
=> false
e = a and b and c
puts "E is #{e}"
=> true

Points to be noted:

  • && has more precedence than =, thus evaluated result is assigned to d
  • and has less precedence than =, thus value of a is directly assigned to e which gets printed.

Wednesday 9 April 2014

Rails Truncate method

       
Today i would like to give some information on Rails api Truncate method.
I was looking for a  method in my Ruby on Rails project which can truncate a string  after some characters.. after searching I found 'truncate' method, which is a rails api method for truncating string in views.
So if you have string
str= "Hi, we are going to truncate any large string by this rails api method??" # 72 characters

the truncate method can be used in the view like this:


truncate(str, :length => 25)


and the result would be :
Hi, we are going to tr...


This was very useful since we had to write seprate ruby methods for truncating.


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

end


Base class
________


class Nokia < Mobile

end


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
    end

  2) Open your rails console

     User.reflect_on_all_associations.map{ |reflection| ":#{reflection.macro} => :#{reflection.name}"}

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