#mongo
validates_associated :episodes validates :episodes, associated: true
validates_format_of :title, with: /\A\w+\Z/ validates :title, format: { with: /\A\w+\Z/ }
# Set the field values in the document. Person.new(first_name: "Jean-Baptiste", middle_name: "Emmanuel") person.attributes = { first_name: "Jean-Baptiste", middle_name: "Emmanuel" }
person.write_attributes( first_name: "Jean-Baptiste", middle_name: "Emmanuel" )
class Foo
include Mongoid::Document
field :bar
end
foo = Foo.new(:bar => "baz")
foo.to_json # => { bar: "baz" }
class User
scope :active, :status => 'active'
scope :over, :lambda { |age| where(:age.gt => age) }
end
User.where(:name => /^J/).active.over(40)
class Article
include Mongoid::Document
field :name, type: String
field :body, type: String
field :slug, type: String
before_create :send_message
after_save do |document|
# Handle callback here.
end
protected
def send_message
# Message sending code here.
end
end
class Person include Mongoid::Document include Mongoid::Timestamps end
//This adds basic behavior for created_at and updated_at fields.
User.where(:name => "John")
User.all(:age.lt => 24, :age.gt => 18)
User.where(:title.in => %w(Ms Mrs))
class User scope :active, :status => 'active' scope :over, :lambda { |age| where(:age.gt => age) } end
User.where(:name => /^J/).active.over(40)
class Spot include Mongoid::Document field :name, :type => String field :latlng, :type => Array index [[:latlng, Mongo::GEO2D]] end
Ruby Spot.create( :name => "Majoran Distillery", :latlng => [41.910973,-87.635544] )
Spot.where(:latlng.near => majoran.latlng) #=> [“Majoran Distillery”, ”Cibo”, “Westpac” ]
Bar.where(:likes.gt => 100).geo_near([ 50, 12 ]).spherical