Skip to content

Instantly share code, notes, and snippets.

@slowjud
Created March 10, 2014 23:55
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/9476959 to your computer and use it in GitHub Desktop.
Save slowjud/9476959 to your computer and use it in GitHub Desktop.
Good Bike models for my mongoid 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, cascade_callbacks: true
# indexes
index brand_name: 1
index style: 1
index colour: 1
index price: 1
index brand_name: 1, style: 1
index 'parts.brand_name' => 1
# scopes
scope :sram_cranks, ->(){
where( 'parts.name' => 'CRANK',
'parts.brand_name' => 'SRAM')
}
end
class Part
include Mongoid::Document
embedded_in :bike
field :name
validates :name, presence: true
field :brand_name
field :colour
validates :colour, presence: true
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