Last active
August 29, 2015 13:57
-
-
Save slowjud/9476981 to your computer and use it in GitHub Desktop.
Dodgy Bike models for my mogoid talk at RORO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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