Skip to content

Instantly share code, notes, and snippets.

@derwiki
Created January 19, 2013 19:30
Show Gist options
  • Save derwiki/4574572 to your computer and use it in GitHub Desktop.
Save derwiki/4574572 to your computer and use it in GitHub Desktop.
class Item < ActiveRecord::Base
attr_accessible :description, :owner_id, :title, :image_url, :weekly, :daily,
:amazon_url, :image, :brand, :component
belongs_to :owner, class_name: 'User'
COMPONENTS = [:lens, :body, :accessory].each do |component|
scope component.to_sym, where(component: component)
end
BRANDS = [:canon, :nikon, :sony, :olympus, :other].each do |brand|
scope brand.to_sym, where(brand: brand)
end
end
1.9.3p327 :005 > Item.canon.body.collect(&:title)
Item Load (0.3ms) SELECT "items".* FROM "items" WHERE "items"."brand" = 'canon' AND "items".
"component" = 'body'
=> ["Canon 60D Body", "Canon Rebel XTi 10.1 MP Body"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment