Skip to content

Instantly share code, notes, and snippets.

@slowjud
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slowjud/9476981 to your computer and use it in GitHub Desktop.
Save slowjud/9476981 to your computer and use it in GitHub Desktop.
Dodgy Bike models for my mogoid talk at RORO
class Bike
include Mongoid::Document
field :brand_name
validates :brand_name, presence: true
STYLES = %w(CARBON_MONSTROSITY DUALIE FIXIE TOURER)
field :style
validates :style, inclusion: STYLES
field :colour
field :price, type: BigDecimal
embeds_many :parts
# indexes
index brand_name: 1
index style: 1
index colour: 1
index price: 1
end
class Part
include Mongoid::Document
embedded_in :bike
field :name
validates :name, presence: true
field :brand_name
field :colour
validates :colour, presence: true
# indexes
index brand_name: 1
# scopes
scope :sram_cranks, ->(){
where( name: 'CRANK',
brand_name: 'SRAM')
}
before_save :upcase_colour
def upcase_colour
self.colour = colour.upcase
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment