Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =begin
- = Find Those With =
- A macro method for defining named_scopes for has_many owners.
- USAGE
- Find only those posts with comments:
- class Post < ActiveRecord::Base
- has_many :comments
- finds_those_with :comments
- end
- Post.with_comments
- DISCLAIMER
- I don't know what the crap I'm doing with SQL.
- =end
- ActiveRecord::Base.class_eval do
- def self.finds_those_with(name, options={})
- options[:using] ||= "with_#{name}"
- options[:model] ||= name.to_s.classify.constantize
- named_scope options[:using].to_sym,
- :include => name,
- :select => "#{table_name}.*",
- :conditions => "#{options[:model].table_name}.id IS NOT NULL"
- end
- end
Add Comment
Please, Sign In to add comment