Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. class ActiveRecord::Base
  2. def self.find_by_proto(many, obj)
  3. cond = ""; conds = []
  4. obj.attributes.each do |k,v|
  5. if v; conds << cond += "#{k} = ?"; conds << v; end
  6. end
  7. self.find(many, :conditions => conds)
  8. end
  9. end
  10.  
  11. # call this like this:
  12.  
  13. myProto = Address.new
  14. myProto.street = 'Main Street'
  15. myProto.city = 'Austin'
  16.  
  17. addresses = Address.find_by_proto(:all, myProto)
  18.  
  19. # addresses is now all Addresses that match the proto
  20. # Useful?
  21. # anybody have a more sophisticated solution, e.g. one that uses RegExps maybe?
Add Comment
Please, Sign In to add comment