Skip to content

Instantly share code, notes, and snippets.

@m4tthumphrey
Last active December 11, 2015 08:19
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 m4tthumphrey/4572661 to your computer and use it in GitHub Desktop.
Save m4tthumphrey/4572661 to your computer and use it in GitHub Desktop.

TL;DR: I want to be able to do this:

features = Product.features

So it returns:

[id: 1, name: 'Colour', value: 'Blue']
[id: 2, name: 'Size', value: 'M']
[id: 3, name: 'Shape', value: 'Round']
class Feature < ActiveRecord::Base
has_many :product_features
has_many :products, :through => :product_features
attr_accessible :name
end
class Product < ActiveRecord::Base
belongs_to :company
has_many :product_features
has_many :features, :through => :product_features
attr_accessible :description, :name, :company
validates :company, :presence => true
end
class ProductFeature < ActiveRecord::Base
belongs_to :product
belongs_to :feature
attr_accessible :value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment