Skip to content

Instantly share code, notes, and snippets.

@liushooter
Last active August 29, 2015 14:18
Show Gist options
  • Save liushooter/f5fd7bc41ad41bf47606 to your computer and use it in GitHub Desktop.
Save liushooter/f5fd7bc41ad41bf47606 to your computer and use it in GitHub Desktop.
💋
# /app/models/mongoid/base_model.rb
module Mongoid
module BaseModel
extend ActiveSupport::Concern
included do
scope :recent, -> { desc(:create_at) }
end
module ClassMethods
def _search_(k, v)
if k && v && v.size > 0
any_of({ k => /.*#{v}.*/i })
else
all
end
end
alias_method :like, :_search_
end
end
end
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::BaseModel
end
User.like(:name, "邹")
User.recent
@zhaiyingchang
Copy link

牛逼!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment