Skip to content

Instantly share code, notes, and snippets.

@jonathan
Created October 5, 2009 20:05
Show Gist options
  • Save jonathan/202400 to your computer and use it in GitHub Desktop.
Save jonathan/202400 to your computer and use it in GitHub Desktop.
class Candidate
include MongoMapper::Document
key :alias, String, :required => true
key :notes, String
many :phones
end
class Phone
include MongoMapper::EmbeddedDocument
TYPES = ['Mobile', 'Home', 'Work'].freeze
key :number, String, :required => true
key :type, String, :required => true, :within => TYPES
belongs_to :candidate
def self.types()
TYPES
end
end
>> c = Candidate.new
=> #<Candidate notes: nil, _id: nil, alias: nil>
>> c.valid?
=> false
>> c.errors
=> #<Validatable::Errors:0x1011e0878 @errors={:alias=>["can't be empty"]}>
>> c.phones
=> []
>> p = Phone.new
=> #<Phone number: nil, _id: "4aca502f0da752c32a000001", type: nil>
>> p.valid?
=> false
>> p.errors
=> #<Validatable::Errors:0x1011a6600 @errors={:type=>["can't be empty"], :number=>["can't be empty"]}>
>> c.phones << p
=> [#<Phone number: nil, _id: "4aca502f0da752c32a000001", type: nil>]
>> c.alias = 'blah'
=> "blah"
>> c.valid?
=> true
>> c.save
=> true
>> c.phones
=> [#<Phone number: nil, _id: "4aca502f0da752c32a000001", type: nil>]
>> c.phones.first.valid?
=> false
>> c.phones.first.errors
=> #<Validatable::Errors:0x1011a6600 @errors={:type=>["can't be empty"], :number=>["can't be empty"]}>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment