我希望把所有认证相关的代码放在定义 Auth “模型”的文件中,就像这样:
在嵌入式
class User
include Mongoid::Document
embeds_one :auth
field :username, type: String
end
class Auth
include Mongoid::Document
embedded_in :user, inverse_of: :auth
field :password
def self.login(user, pass)
User.first(conditions: { username: user, password: pass })
end
end
问题在于:无法调用嵌入文档的类方法:
> Auth.login('user', 'pass')
Mongoid::Errors::InvalidCollection: Access to the collection for Auth is not allowed since it is an embedded document, please access a collection from the root document.
> User.auth.login('user', 'pass')
NoMethodError: undefined method `auth' for User:Class
在嵌入式
Mongoid::Document
模型中使用单例方法可能并不是一个好主意?