Skip to content

Instantly share code, notes, and snippets.

@dabit
Last active December 12, 2015 01:38
Show Gist options
  • Save dabit/4692168 to your computer and use it in GitHub Desktop.
Save dabit/4692168 to your computer and use it in GitHub Desktop.
Rails Issue #8811
source "https://rubygems.org"
gem "sqlite3"
gem "rails", :git => 'https://github.com/rails/rails.git', :branch => '3-2-stable'
require 'bundler'
Bundler.require
require 'active_record'
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define(:version => 0) do
create_table "categories", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "product_category_relations", :force => true do |t|
t.integer "product_id"
t.integer "category_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "products", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
end
class Category < ActiveRecord::Base
has_many :product_category_relations, :dependent => :destroy
end
class Product < ActiveRecord::Base
has_many :categories, :through => :product_category_relations
has_many :product_category_relations, :dependent => :destroy
end
class ProductCategoryRelation < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
cat1 = Category.create!(:name => 'category1')
cat2 = Category.create!(:name => 'category2')
p = Product.new(:name => 'product1')
p.categories << [cat1, cat2]
p.save!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment