Ruby On Rails Questions et reponses d'entretien
Question : What is Scope in Ruby on Rails?Reponse :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"
endNow you can simply access the published posts as: Post.published @posts = Post.published Also, you can access recent posts as @recent_posts = Post.recent |
Enregistrer pour revision
Ajoutez cet element aux favoris, marquez-le comme difficile ou placez-le dans un ensemble de revision.
Connectez-vous pour enregistrer des favoris, des questions difficiles et des ensembles de revision.
Les plus utiles selon les utilisateurs :
- What is Ruby On Rails?
- Why Ruby on Rails?
- Explain how (almost) everything is an object in Ruby.
- What are Gems and which are some of your favorites?
- How would you declare and use a constructor in Ruby?