Created
August 24, 2011 21:41
-
-
Save ajsharp/1169333 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
| class Credential | |
| include Mongoid::Document | |
| end | |
| class Credential::Password < Credential | |
| field :password | |
| field :email | |
| end | |
| class User | |
| include Mongoid::Document | |
| embeds_many :credentials do | |
| # Build a new password credential object | |
| # | |
| # @param [Hash] args a hash of arguments passed to the Credential::Password constructor | |
| # @return [Credential::Password] | |
| def build_password(args = {}) | |
| base.credentials.build(args, Credential::Password) | |
| end | |
| end | |
| end | |
| user = User.new | |
| credential = user.credentials.build_password(:password => "password", :email => "user@example.com") | |
| user.credentials.include? credential # => true | |
| user.save! | |
| user.reload.credentials # => [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment