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.