Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.63 KB | None | 0 0
  1. # server.rb
  2. require 'sinatra'
  3. require "sinatra/namespace"
  4. require 'mongoid'
  5.  
  6. # DB Setup
  7. Mongoid.load! "mongoid.config"
  8.  
  9. # Models
  10. class Book
  11.   include Mongoid::Document
  12.  
  13.   field :title, type: String
  14.   field :author, type: String
  15.   field :isbn, type: String
  16.  
  17.   validates :title, presence: true
  18.   validates :author, presence: true
  19.   validates :isbn, presence: true
  20.  
  21.   index({ title: 'text' })
  22.   index({ isbn: 1 }, { unique: true, name: "isbn_index" })
  23.  
  24.   scope :title, -> (title) { where(title: /^#{title}/) }
  25.   scope :isbn, -> (isbn) { where(isbn: isbn) }
  26.   scope :author, -> (author) { where(author: author) }
  27. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement