Guest User

Untitled

a guest
May 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. ##acts_as_xapian tip - using with association proxies
  2.  
  3. ##Named scope
  4. class Lesson < ActiveRecord::Base
  5. named_scope :find_with_xapian, lambda { |*args| {:conditions => ["lessons.id in (?)", ActsAsXapian::Search.new([Lesson], args.to_s).results.collect{|x| x[:model].id}]}}
  6. end
  7.  
  8. ##But if we do this instead, we can now do association proxies
  9. class Lesson < ActiveRecord::Base
  10. def self.find_with_xapian(search_term, options={})
  11. ActsAsXapian::Search.new([self], search_term, options).results.collect{|x| x[:model]}.compact
  12. end
  13. end
  14.  
  15. current_user.lessons.find_with_xapian "something"
  16.  
  17. ##That would automatically scope and return lessons that belong to the current user only. Awesome!
  18.  
  19. ###
  20. #=> Overbryd
  21. # Without compact this leaves sometimes some nil elements in the array, compact returns the array without nil elements.
Add Comment
Please, Sign In to add comment