Last active
December 12, 2015 01:38
-
-
Save dabit/4692168 to your computer and use it in GitHub Desktop.
Rails Issue #8811
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
source "https://rubygems.org" | |
gem "sqlite3" | |
gem "rails", :git => 'https://github.com/rails/rails.git', :branch => '3-2-stable' |
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
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