Skip to content

Instantly share code, notes, and snippets.

@maxcal
Last active May 16, 2020 10:27
Show Gist options
  • Save maxcal/385a5c4f8d85557887602538c4cde5a2 to your computer and use it in GitHub Desktop.
Save maxcal/385a5c4f8d85557887602538c4cde5a2 to your computer and use it in GitHub Desktop.
class AddFeatureGroupIdToFeatures < ActiveRecord::Migration[6.0]
def change
add_refererence :features, :feature_group, foreign_key: true
end
end
class Feature < ApplicationRecord
has_many :products, through: :product_features
has_many :product_features
belongs_to :feature_group
end
class FeatureGroup < ApplicationRecord
has_many :features
has_many :products, through: :features
end
class ProductFeature < ApplicationRecord
belongs_to :feature
belongs_to :product
# default scope should be avoided
end
class Product < ApplicationRecord
has_many :product_features
has_many :features, through: :product_features
has_many :feature_groups, through: :features
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment