#Ruby Class 2: Why Attribute Accessor?

In class 1 we defined the instance variables and we saw how difficult to accessing it. We managed to access it via calling methods that initializing it.
So we can simplify it by defining access methods.

class Test
 def initialize
  @one = 1
 def get_one

The ‘get_one’ method is the reader method for reading the instance variable value.

Ruby has shortcut for this.

class Test
 attr_reader :one
 def initialize
  @one = 1

These accessor methods are identical to the methods we wrote by hand earlier.

Sometimes we need to modify the value of these variables from outside. how we do that?

At first we can do this by hand:

class Test
 def initialize
  @one = 1
 def one=(new_value)
   @one = new_value

The above code allows us to call ‘equal to’ sign on the objects method. This is the setter method. We can set a value to @one like:

t = Test.new
t.one = 3
=> 3

Ruby also provides a shortcut for this. It is: ‘attr_writer’, so rewrite the above code:

class Test
 attr_writer :one
 def initialize
  @one = 1

Most of the cases we needed ‘attr_reader’ and ‘attr_writer’. So the code becomes:

class Test
 attr_reader :one
 attr_writer :one

 def initialize
  @one = 1

We can again simplify the above code!

class Test
 attr_accessor :one

 def initialize
  @one = 1

How is it? Nice. is n’t it?


#Ruby Class 1: Ruby’s instance variable and initialize method simplified

We read so many documents about ruby but do you think about some facts that we really don’t know. See below.

1. Instance variables are accessible across the class for that instance


class Test2
 def one
  @one = 1
 def two
  @two = 2


t = Test.new
t.one is what ?
t.two is what ?

Can you guess?

t.one is nil why? t.one will return @two, but it is not initialized yet. We have to call t.two for intitializing it (the variable gets a value).

t.two is what?
Ans: 1
Why? because we already initialized @one by calling t.one above. If not it will also return nil.

So what is the purpose of instance variable if we are not getting its value when initializing the Class (say t = Test.new)

Here is the importance of ‘initialize’ method. Lets change the method named ‘two’ to ‘initialize’

class Test3
 def one
  @one = 1
 def initialize
  @two = 2

then you try:

t = Test.new
=>    ### See here already declared the variable @two

=> 2

Nice. So we can access that instance variable anywhere in the class.

Mongodb how to Import / Export

For Importing a mongodb use the following command

$ mongodump --db database_name

This will dump the json/bson files into dump/db_name folder
Or specify a directory with -o option

$ mongodump --db database_name -o path_to_folder

By specifying username and password

$ mongodump --db database_name -o /path/to/folder/ --username=my_user --password="my_password"

For Exporting a mongodb use the following command

$  mongorestore --db database_name path_to_the_json_bson_files

path_to_the_json_bson_files => That we already imported and stored before.

Sending Gmail calendar events in Rails 4

Here I am telling about how to send gmail calendar events to user emails. I am using Rails 4.2 and Ruby 2.2.1

For this we can use the best available code package the ‘icalnder’ gem.

Icalender Github

In Gemfile add:

gem 'icalendar'

Suppose we are using user_mailer.rb for sending emails in this case. And we are sending emails to manager and a participant when a conversation is scheduled between them.

I created a method as below.

in user_mailer.rb

   def conversation_scheduled(conversation_id, opts={})
      @conversation = Conversation.find_by_id(conversation_id)
      @manager = @conversation.try(:manager)
      @participant = @conversation.try(:participant)
      @base_url = opts['base_url']

      if opts[:send_to] == 'manager'
        cal = calender_event(@conversation, @manager, "Conversation is scheduled with #{@participant.full_name}", "You have scheduled a conversation with Mr/Mrs #{@participant.full_name} on #{@conversation.starting_time}")  
        attachments['conversation_sheduled.ics'] = { :mime_type => 'text/calendar',
                              :content => cal.to_ical }

        mail(to: @manager.try(:email),
             subject: "Conversation is scheduled with #{@participant.full_name}",
             template_name: 'scheduled_manager')
      elsif opts[:send_to] == 'participant'
        cal = calender_event(@conversation, @manager, "Conversation is scheduled with #{@manager.full_name}", "A conversation is scheduled with Mr/Mrs #{@manager.full_name} on #{@conversation.starting_time}")  
        attachments['conversation_sheduled.ics'] = { :mime_type => 'text/calendar',
                              :content => cal.to_ical }

        mail(to: @participant.try(:email),
             subject: "Conversation is scheduled with #{@manager.full_name}",
             template_name: 'scheduled_participant')

def calender_event(conversation, manager, summary, description)
      cal = Icalendar::Calendar.new
      cal.event do |e|
        e.dtstart     = Icalendar::Values::Date.new(conversation.startime.to_date.strftime("%Y%m%dT%H%M%S%Z"))
        e.dtend       = Icalendar::Values::Date.new(conversation.end_time.strftime("%Y%m%dT%H%M%S%Z"))
        e.summary     = summary
        e.description = description
        e.ip_class    = "PRIVATE"
        e.organizer = Icalendar::Values::CalAddress.new("mailto:#{manager.try(:email)}", cn: manager.try(:full_name))

All we need to do is send an attachment to the email setting the proper mime type to ‘text/calendar’ and the content by calling the ‘to_ical’ method on the event and it returns the calender object.

With proper email templates I do receive the emails with Calendar events and I can add this event to my google calendar. I can update the event details etc.

#Rails 4.2 #Ruby2.2 How to find association class and other info from an object and its association name

When I was doing something, I encountered one situation like: I need the association class of an object. I have the object and its association name as input. How can I find the association class?

Suppose we have Student class that belongs to a school

class School
  has_many students

class Student
  belongs_to :school

and so many other relations like this in my project.

So I have

s = Student.last
:school symbol 

in my hand

I can use

s.school.class and s.school.class.name

But what if the school is blank? The result is ‘NilClass’ From the above code.

Basically for has_many associations we gets the class name as


because recently in new versions rails changed the Array of objects as associations to its own ‘CollectionProxy’.

So we can use ‘ActiveRecord::Reflection::ClassMethods’ for finding all the association info.

This Rails module is so useful to find all the association related information.

In the above situation we can use ‘reflect_on_association’ method for finding association reflection info. And it returns ‘ActiveRecord::Reflection’ Object.


> s.class.reflect_on_association(:school)
=> ##<ActiveRecord::StatementCache:0x000000037a5c08 
@query_builder=#, @bind_map=#<ActiveRecord::StatementCache::BindMap:0x000000037a7238 
@indexes=[0], @bind_values=[[#
, #
]]>>}, @scope_lock=#
, @class_name="Topic", @foreign_key="school_id">

#Rails Validation: belongs_to association, How to validate effectively?

Hi guys, how to validate a Rails belongs_to association in most effective way? Did you think about this anytime? I am asking this question because there is something that most of the people will not notice. I will explain here.

Suppose we have 2 Models User and Group

Every User must belongs to one of the group.

So how we validate this? The normal way that everyone follows is given below.

Validate foreign key

class User
  belongs_to group
  validates :group_id, presence: true

What this code actually does? Lets find out.

> Group.last.id # => 80
> User.new(group_id: 100).valid? # => true
> Group.exists?(100) # => nil

Whats that? Is it true? Not really. So rails fooling us? Haha. The presence validator for foreign key will only check system provides any value to it. Thats it. And it is not cared about the existence of that record. So what we will do for that? Lets check the second method.

Validate associated record

class User
  belongs_to group
  validates :group, presence: true

Here what the validator does is in the background it checks the record exists or not. If record not exists it returns false for valid? method.

> Group.last.id # => 80
> user = User.new(group_id: 100)
> user.valid? # => false
> Group.exists?(100) # => nil
> user.errors.full_messages # => ["Group can't be blank"]

Wahh..great! But wait…think… What it actually does? Whenever the user record goes through the valid? method, in the background rails fire a query to the database to find this record exists or not. Is that good? In some way, it is bad. It affects performance.

But we have to consider database integrity. So the second approach wins even though it is firing an extra query in the background.

PostgreSQL 9.3 : Installation on ubuntu 14.04

Hi guys, I just started installing postgres on my ubuntu VM. I referred some docs, and followed this one: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-14-04

Its pretty much explained in this page. But just explaining here the important things.

You can install postgres by ubuntu’s own apt packaging system. Update local apt repository, if u want.

$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib

Postgres uses role based access for the unix users. After the installation a default role called ‘postgres’ will be created. You can login to postgres account and start using or creating new roles with Postgres.

Sign in as postgres user

$ sudo -i -u postgres

Access the postgres console by

$ psql

But i cannot enter into the console and I got the following error:

postgres@8930a29k5d05:/home/rails/my_project$ psql
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

What could be the reason for this error?

So just gone through Postgres doc (http://www.postgresql.org/docs/9.3/static/server-start.html). You can see the same error under the section 17.3.2. Client Connection Problems. But the solution is not mentioned.

Original Reason: PostgreSQL Server was not running after the installation.

I tried to reboot and thought, via init script the server will run automatically. That also not happened. So I understood that something prevents postgres from running the server. What is it?

Just check your postgres server is running or not

$ sudo -aux | grep post
postgres@8930a29k5d05:/home/rails/my_project$ ps -aux | grep post
root       136  0.0  0.2  47124  3056 ?        S    06:10   0:00 sudo -u postgres -s
postgres   137  0.0  0.3  18164  3220 ?        S    06:10   0:00 /bin/bash
postgres   140  0.0  0.2  15572  2192 ?        R+   06:10   0:00 ps -aux
postgres   141  0.0  0.0   4892   336 ?        R+   06:10   0:00 grep post

The server is not running.

Run the server manually by

root@8930a29k5d05:/home/rails/my_project#  /etc/init.d/postgresql start
 * Starting PostgreSQL 9.3 database server
                                                                                                                                                         [ OK ] 
root@8930a29k5d05:/home/rails/my_project# ps aux | grep post
postgres   158  0.1  2.0 244928 20752 ?        S    06:28   0:00 /usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf
postgres   160  0.0  0.3 244928  3272 ?        Ss   06:28   0:00 postgres: checkpointer process

postgres   161  0.0  0.4 244928  4176 ?        Ss   06:28   0:00 postgres: writer process

postgres   162  0.0  0.3 244928  3272 ?        Ss   06:28   0:00 postgres: wal writer process

postgres   163  0.0  0.5 245652  6000 ?        Ss   06:28   0:00 postgres: autovacuum launcher process

postgres   164  0.0  0.3 100604  3336 ?        Ss   06:28   0:00 postgres: stats collector process

root       178  0.0  0.0   8868   884 ?        S+   06:28   0:00 grep --color=auto post

Now it is working. If still not works, then try to reconfigure your locales as mentioned here

$ dpkg-reconfigure locales

It is strange that, after installing such a popular db software, it doesn’t provide any information regarding the failure of its own server. It should give the developers some clue so that they can save their precious time.

The reason of this failure, that I concluded is
1. After installation we have to run the server manually
2. I tried resetting the locales (So if no locales set in the machine may prevented the postgres from starting automatically?)