jump to navigation

Ruby on Rails Interview Questions October 3, 2008

Posted by Puneet Pandey in Ruby On Rails.
Tags: , , ,
trackback

Hi Guys, Hope you all are fine, Well I think that I should Post something different in my blog, so that It will help you to get some more knowledge :) , So I came with Interview Questions, Hope this will help you a lot, I will update it regularly, If I found something Interesting. Well If you are experienced, I hope this should help you. All the Questions are dynamically Typed.

Q1. What is request.xhr?
Sol: A request.xhr tells the controller that the new Ajax request has come, It always return TRUE or FALSE

Q2. What is the Difference between Static and Dynamic Scaffolding?
Sol: The Syntax of Static Scaffold is like this:
ruby script/generate scaffold User Comment
Where Comment is the model and User is your controller, So all n all static scaffold takes 2 parameter i.e your controller name and model name, whereas in dynamic scaffolding you have to define controller and model one by one.

Q3. What is the Difference between Symbol and String?

Q4. What is Session and Cookies?
Sol: Session: are used to store user information on the server side.
cookies: are used to store information on the browser side or we can say client side
Session : say session[:user] = “puneet” it remains when the browser is not closed

Q5. Why Ruby on Rails?
Sol: There are lot of advantages of using ruby
1. DRY Principal
2. Convention over Configuration
3. Gems and Plugins
4. Scaffolding
5. Pure OOP Concept

Q6. What is MVC? and how it Works?
Sol: MVC tends for Model-View-Controller, used by many languages like PHP, Perl, Python etc. The flow goes like this: Request first comes to the controller, controller finds and appropriate view, your view interacts with model, model interacts with your database, for Example your url is something like this:
http://localhost:3000/users/new
here users is your controller and new is your method, there must be a file in your views/users folder named new.html.erb, so once the submit button is pressed, User model or whatever defined in the rhtml form_for syntax, will be called and values will be stored into the database.

Q7. What things we can define in the model?
Sol: There are lot of things you can define in models few are:
1. Validations (like validates_presence_of, numeracility_of, format_of etc.)
2. Relationships(like has_one, has_many, HABTM etc.)
3. Callbacks(like before_save, after_save, before_create etc.)
4. Suppose you installed a plugin say validation_group, So you can also define validation_group settings in your model
5. ROR Queries in Sql

Q8. What is ORM in Rails?
Sol: ORM tends for Object-Relationship-Model, it means that your Classes are mapped to table in the database, and Objects are directly mapped to the rows in the table.

Q9. How many Types of Relationships does a Model has?

Q10.  What is the difference between rails version 2.2.2 with the older ones?

Q11. Difference between render and redirect?
Sol: render example: render :action, render :partial etc.
redirect example: redirect_to :controller => ‘users’, :action => ‘new’

Q12. How to use sql db or mysql db. without defining it in the database.yml
Sol. http://stuff.lilleaas.net/active_record_anywhere

Q13. What are helpers and how to use helpers in ROR?
Sol. Helpers (“view helpers”) are modules that provide methods which are automatically usable in your view. They provide shortcuts to commonly used display code and a way for you to keep the programming out of your views. The purpose of a helper is to simplify the view. It’s best if the view file (RHTML/RXML) is short and sweet, so you can see the structure of the output.

Q14. What is Active Record?
Sol. Active Record are like Object Relational Mapping(ORM), where classes are mapped to table and objects are mapped to colums in the table

Q15. Ruby Supports Single Inheritence/Multiple Inheritence or Both?
Sol. Ruby Supports only Single Inheritnece

Q16. How many types of callbacks available in ROR?
Sol. * (-) save
* (-) valid
* (1) before_validation
* (2) before_validation_on_create
* (-) validate
* (-) validate_on_create
* (3) after_validation
* (4) after_validation_on_create
* (5) before_save
* (6) before_create
* (-) create
* (7) after_create
* (8) after_save

Q17. Suppose in one of my method I am updating the attributes of table, in my model I have defined after_create do X, and after_save do Y. Which method will be called?

Ruby Interview Questions :

Q1. How to use two database into a Single Application?
Sol. http://magicmodels.rubyforge.org/magic_multi_connections/, According to this link : ActiveRecord models are allowed one connection to a database at a time, per class. Ruby on Rails sets up the default connection based on your database.yml configuration to automatically select development, test or production.
But, what if you want to access two or more databases – have 2+ connections open – at the same time. ActiveRecord requires that you subclass ActiveRecord::Base.
That prevents you doing migrations from one database to another. It prevents you using one set of model classes on two or more databases with the same schema.
Magic Multi-Connections allows you to write your models once, and use them for multiple Rails databases at the same time. How? Using magical namespacing.

screenshot

To do this :
[A] sudo gem install magic_multi_connections
[B] require ‘magic_multi_connections’
Add the following to the bottom of your environment.rb file
You can also find examples on this link : http://magicmodels.rubyforge.org/magic_multi_connections/

Q2. What is the Notation used for denoting class variables in Ruby?

Q3. What is the use of Destructive Method?

Q4. What is the use of load and require in Ruby?

Q5. What is the use of Global Variable in Ruby?

Q6. How does nil and false differ?

Q7. How is visibility of methods change in Ruby?

Q8. What is a Class Instance Variable

Q9. What are the rules and conventions to be followed in Ruby for naming a method?

Q10. What is the use of Super?

Q11. How is class method defined in Ruby?

Q12. What are the Operators available in Ruby?

Q13. What are the looping structure available in Ruby?

Q14. What is the scope of local variable?

Q15. What are the OOP supported by Ruby?

Q26. If Ruby over PHP, Why?

Q17. Garbage collection in Ruby?

Q18. Environment Variables in Ruby?

Q19. What are Float, Dig and Max?

Q20. What is Ruby Code blocks?

Q21. What kind of conditions ruby support?

Q22. Difference between puts and print

More Questions and Answers of this will be published very soon :) So stay in touch.. I will keep you updating.. if you have some questions please reply…

Comments»

1. Anuj Gupta - October 13, 2008

Hey Puneet,
Nice collection of Questions you have, but I am waiting for the answers of it. Hope you will publish it soon.

Cheers..

2. Ujjwal Trivedi - October 31, 2008

Thats a nice collection puneet! I have been compiling a list myself this one would be a great reference… Thanks!

3. Paddy - December 9, 2008

How many Types of Relationships does a Model has?

1. Belongs to
2. Has many / has one
3. has and belongs to many (HABTM)

A pictorial representation of the relationship has been provided here.

http://www.scribd.com/doc/66259/Active-Record-Relationships-A-Ruby-on-Rails-Cheat-Sheet

4. Puneet Pandey - December 9, 2008

Hey Dear,
Thanks for giving answer, I am seeking for few more, so that it will be easy for others to see… any help from your side would be highly appreciated

5. Paddy - December 9, 2008

What is the Notation used for denoting class variables in Ruby?

@@

6. Ramu - December 10, 2008

Thanks for helping others to learn what you already know.

7. Puneet Pandey - December 10, 2008

Thanks Ramu for Appreciating my work, definitely it will help others. Still there are lot of questions which needs to be answered, I m searching and also getting responses from ror guys :)

8. Santosh - January 20, 2009

Hi Puneet,

Thanks for the questions. I am just started learning ROR from last 1 month. It is a
nice reference for me. Good work keep going. Can i use scaffold to show data from two tables i.e two models and one controller ?

Santosh

9. Puneet Pandey - January 20, 2009

hey Santosh Thanks a ton for appreciating my work. I will carry this on as I am receiving lot of good responses on this.

About your problem, yes you can display data from n number of tables through one controller.

you have to go like this:

def display_data
@comments = Comment.find(:all, :conditions => ["Some Condition"])
@posts = Post.find(:all, :conditions => ["Some Conditions"])
end

Then you have to start a loop to retrieve each value individually.

Hope this helps :)

10. Santosh - January 20, 2009

Thanks puneet

11. Puneet Pandey - January 21, 2009

You can also do eager loading, I am not very much familiar about this concept, but it will save lots of time, see how it works:

def display_data
generate_comment_post_lists
end

private
def generate_comment_post_lists
@comments = Comment.find(:all, :conditions => ["Some Condition"])
@posts = Post.find(:all, :conditions => ["Some Conditions"])
end

Hope it helps :)

12. santosh - January 21, 2009

Hi puneet
Thanks for the help. Can u mail me on my mail id its santoshror@gmail.com. I need u r mail id.

13. Ganesh - February 12, 2009

Hi punnet,

Thanks for the questions. I working & learning ROR 2.6 from last 6 month. Getting gr8 reference for me. You really did a good work.
I need your help..! Can I fetch address other than the Gmail, Yahoo, Hotmail by Contacts gem?

Can u mail me mail-Id…?

Thanks
Ganesh K.
- ganesh.kathare@wwindia.com

14. Puneet Pandey - February 12, 2009

Hi Ganesh,

Thanks a ton for appreciating what am I posting on ruby on rails.. its good to see that you guys are getting help from my posts…

will continue that… :)

I think you are able to fetch only Gmail, Hotmail and Yahoo contacts from contacts gem.

If you are trying to fetch contacts from Facebook, there is a facebooker plugin available for that.

But you have not cleared in your comment that apart from 3 where do you want to fetch contacts from.

Cheers :)
Puneet

15. Phani - February 23, 2009

Hi punnet,

My name is Phani, i need the same thing which ganesh posted, i need to import the contacts from AOL, facebook, orkut and etc( including the default set Yahoo-Gmail-Hotmail.)
For a social application…..

Thanks
Phani

16. Puneet Pandey - February 23, 2009

Hi Ganesh and Phani,

Sorry I was busy in some work.. well I don’t know how to fetch contacts from AOL, Rediff etc. I will look at it and let you guys know soon about it.. but yes I have done how to fetch contacts from Facebook..

There is one plugin available for this “Facebooker”.. u need to install that using

ruby script/plugin install git://github.com/mmangino/facebooker.git

I will soon publish a post on this probably in a day or two… meanwhile go through the docs and I will keep you updating :)

Regards
Puneet

17. RubyGuy - March 9, 2009

Guys,

Anyone know how Ruby integrates with .Net code?

18. Ganesh - June 19, 2009

Hi puneet,
I have implemented method of Contacts Gem.. Working very fine.
Thank you 4 provided Contacts Gem article,
Nice work..!

Now I’m facing problem with File download service.. I’m working on resume upload service.. So that I want to make functionality by which will provide a link which get download that particular file from its source location where its is actually uploaded one..

In short need help to create File Download Service(with File Download dialog Box)

Thank u.

19. Puneet Pandey - June 19, 2009

Hi ganesh,

For your reference I would like to tell you that ruby provides one method called “send_file”

The syntax would be like: send_filev “Location of the FIle”, :type=> ‘text/doc; charset=utf-8; header=present’, :disposition=>’inline’, :filename=>”Your File Name”

Please look at this url for your reference:
http://api.rubyonrails.org/classes/ActionController/Streaming.html#M000443

Let me know if that helps

Cheers!!

20. Ganesh - June 20, 2009

Thanks Puneet,
It worked for my task.. But did some modifications with the syntax…
like
send_file “public/data/#{@product.f_name}”, :type=> ‘text/doc; charset=utf-8; header=present’, :disposition=>’inline’

Where – “public/data” is file location in the project and
@product.f_name -> file name retrieving from the db.
Now here no need to pass filename for :filename => ….!

Thanks 4 co-operation.
Cheers!!!!!!!!

- Ganesh Kathare.

21. Puneet Pandey - June 23, 2009

@ganesh,

Looks good that your code is up and running :)

Cheers!!

22. Ganesh - June 26, 2009

Hi Puneet,

Today, I come with some more problem where I need your help..!
1)
SMS Integration:
I’m working on applications which will provide service for Mobile No verification by SMS. If number get verify by replied SMS then only he/she can register on the site..! So, in this appln I have to integrate SMS service with its API..But dont know how do I Implement??
What are the settings it requires in ROR??

I also have searched this with many help blog on the net.. But not getting it exactly..!
- I got A/c n API key of ZNISMS.com (paid service.)

2)
Payment Gateway Integration:
For this I stucked on the point where and how do I integrate Payment gateway??? in the application while implementing transaction part of the apllications Buy/Sale service..

In the both service I have to interprete with third party service… What n How in ROR??

Please Help..!!!

Regards,
-Ganesh K.

23. Ganesh - June 29, 2009

Hey puneet,
I got the solution for 1st problem, Now expected service working very fine..
But still I stucked @ 2nd point.. So still doing R&D on it..!
Let me know if you got any solution regarding to 2nd problem..!
Got some relief due to SMS Integration..!

Thanks n Cheers..!!
_Ganesh Kathare.

24. Puneet Pandey - June 29, 2009

hey ganesh,

sorry for replying late dude as I got engaged with some work.. Ganesh I haven’t worked on SMS integration part(first of all), secondly I can’t even refer you some guy who has worked on it, coz I don’t know..

Better you try to figure out urself.. and post in your blog so that it will be helpful for others…

Regards
Puneet Pandey

25. Ganesh - July 14, 2009

Hi puneet,

Need help for file(imagefile) uploading using ajax

I made something like

## myview.html.erb

{:action => “uploadimage”}, :html => {:multipart => true}) do %>

## with controller
def uploadimage
image = params[:custom][:image]
@custom = Custom.new(@user,image,”greeting1″)
@custom.save
end

I can upload file using form_tag with same code.. But not getting result with form_remote_tag..

So, can you help..??

Thanks,
-Ganesh K

26. Ganesh - July 14, 2009

Hi puneet,

Need help for file(imagefile) uploading using ajax

I made something like

## myview.html.erb
{:action => “uploadimage”}, :html => {:multipart => true}) do %>

## with controller
def uploadimage
image = params[:custom][:image]
@custom = Custom.new(@user,image,”greeting1″)
@custom.save
end

I can upload file using form_tag with same code.. But not getting result with form_remote_tag..

So, can you help..??

Thanks,
-Ganesh K

27. Ganesh - July 14, 2009

Hi puneet,

Need help for file(imagefile) uploading using ajax

I made something like

## myview.html.erb
{:action => “uploadimage”}, :html => {:multipart => true}) do %>

## with controller
def uploadimage
image = params[:custom][:image]
@custom = Custom.new(@user,image,”greeting1″)
@custom.save
end

I can upload file using form_tag with same code.. But not getting result with form_remote_tag..

So, can you help..??

Thanks,
-Ganesh K