Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

Ruby On Rails Interview Questions and Answers

Ques. 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 <= "+

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 <= ?",]
  scope :recent, :order => "posts.published_at DESC"

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? Yes No

Most helpful rated by users:

©2020 WithoutBook