Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby On Rails Interview Questions and Answers

Ques 16. How can you call the base class method from inside of its overridden method?

If you are inside the overridden method in the derived class then a simple call to super will call the right method in the base class

class Parent
   def try_this()
      puts "parent"
   end
end

class Child < Parent
   def try_this()
      super()
      puts "child"
   end
end

ch = Child.new
ch.try_this()

This generates the output

parent
child

Now if you just want to call the base class without calling the derived class then the best way to do that is to simply assign an alias to the parent method like this:

class Parent
  def knox
    puts 'parent'
  end
end
 
class Child < Parent
   alias_method :parent_knox, :knox
   def knox
     puts 'child'
   end
end
 
ch = Child.new
ch.parent_knox
ch.knox

This allows you to call the base class method with the alias parent_knox and the derived class method knox can be called directly.

parent
child

Is it helpful? Add Comment View Comments
 

Ques 17. Define the Rails MVC implementation with an example.

The MVC framework is an age-old architecture pattern that works very well for most applications. Rails has adopted the MVC pattern in its inherent design.

Stated Simply:

a) Model — is where the data is — the database

b) Controller — is where the logic is for the application

c) View — is where the data is used to display to the user

Is it helpful? Add Comment View Comments
 

Ques 18. What is Scope in Ruby on Rails?

Scopes are nothing more than SQL scope fragments. By using these fragments one can cut down on having to write long queries each time you access content.

Say you typically access content as shown below:

@posts = Post.where("published_at IS NOT NULL AND posts.published_at <= "+ Time.now)

Ruby offers you a nice way to put the where condition inside a scope statement as shown below.

class Post < ActiveRecord::Base
  scope :published, lambda { 
    { :conditions =>
      ["posts.published_at IS NOT NULL AND posts.published_at <= ?", Time.now]
    }
  }  
  scope :recent, :order => "posts.published_at DESC"
end

Now you can simply access the published posts as: Post.published

@posts = Post.published

Also, you can access recent posts as 

@recent_posts = Post.recent

Is it helpful? Add Comment View Comments
 

Ques 19. CAN YOU GIVE AN EXAMPLE OF A CLASS THAT SHOULD BE INSIDE THE LIB FOLDER?

Modules are often placed in the lib folder. 

Is it helpful? Add Comment View Comments
 

Ques 20. WHERE SHOULD YOU PUT CODE THAT IS SUPPOSED TO RUN WHEN YOUR APPLICATION LAUNCHES in Ruby on Rails?

In the rare event that your application needs to run some code before Rails itself is loaded, put it above the call to require ‘rails/all’ in config/application.rb.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook