SQLTerminal.app
now available

XPath.app
now available
Want fresh tech tips in your inbox?

named_scope in Rails

       

Take advantage of the named_scope method in your models and make find queries simple and beautiful!


#app/models/comment.rb
class Comment < ActiveRecord::Base
  belongs_to :post
  named_scope :pending, :conditions => ["pending = ?", true]
  named_scope :for, lambda { |*args| {:include => :post,
      :conditions => ["posts.account_id = ?", (args.first||0)] }}
end

#app/models/post.rb
class Post < ActiveRecord::Base
  has_many :comments
end

Now you can get all comments for your posts that are marked as pending with this method:

@comments = Comment.pending.for(@account.id)
Tagged w/ #ruby on rails

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app