Last active
August 29, 2015 14:18
-
-
Save liushooter/f5fd7bc41ad41bf47606 to your computer and use it in GitHub Desktop.
💋
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
牛逼!