Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active April 29, 2016 16:13
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 JoshCheek/c3af31ee610720ec725f2896a0e38ec4 to your computer and use it in GitHub Desktop.
Save JoshCheek/c3af31ee610720ec725f2896a0e38ec4 to your computer and use it in GitHub Desktop.
Many to Many
# https://github.com/BrantDFaulkner/database_schema
require 'active_record'
require 'logger'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.logger = Logger.new $stdout
ActiveSupport::LogSubscriber.colorize_logging = false
ActiveRecord::Schema.define do
self.verbose = false
# MIGRATION
create_table :teams do |t|
t.string :name
end
create_table :fans do |t|
t.string :name
end
create_table :fans_teams do |t|
t.integer :team_id
t.integer :fan_id
end
end
# MODELS
class Team < ActiveRecord::Base
has_many :fans_team
has_many :fans, through: :fans_team
end
class Fan < ActiveRecord::Base
has_many :fans_team
has_many :teams, through: :fans_team
end
class FansTeam < ActiveRecord::Base
belongs_to :team
belongs_to :fan
end
# SEEDS (or tests or controller or console or w/e)
white_socks = Team.create! name: 'White Socks'
cubs = Team.create! name: 'Cubs'
Fan.create! name: 'Jim', teams: [white_socks, cubs]
Fan.create! name: 'Sally', teams: [white_socks]
Fan.create! name: 'Charlie', teams: [cubs]
Team.first # => #<Team id: 1, name: "White Socks">
.fans # => #<ActiveRecord::Associations::CollectionProxy [#<Fan id: 1, name: "Jim">, #<Fan id: 2, name: "Sally">]>
.last # => #<Fan id: 2, name: "Sally">
.teams # => #<ActiveRecord::Associations::CollectionProxy [#<Team id: 1, name: "White Socks">]>
.first # => #<Team id: 1, name: "White Socks">
.fans # => #<ActiveRecord::Associations::CollectionProxy [#<Fan id: 1, name: "Jim">, #<Fan id: 2, name: "Sally">]>
.last # => #<Fan id: 2, name: "Sally">
.name # => "Sally"
# >> D, [2016-04-29T11:10:35.881889 #94256] DEBUG -- : (0.3ms) CREATE TABLE "teams" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar)
# >> D, [2016-04-29T11:10:35.882344 #94256] DEBUG -- : (0.1ms) CREATE TABLE "fans" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar)
# >> D, [2016-04-29T11:10:35.882613 #94256] DEBUG -- : (0.1ms) CREATE TABLE "fans_teams" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "team_id" integer, "fan_id" integer)
# >> D, [2016-04-29T11:10:35.903784 #94256] DEBUG -- : (0.0ms) begin transaction
# >> D, [2016-04-29T11:10:35.906699 #94256] DEBUG -- : SQL (0.1ms) INSERT INTO "teams" ("name") VALUES (?) [["name", "White Socks"]]
# >> D, [2016-04-29T11:10:35.906995 #94256] DEBUG -- : (0.0ms) commit transaction
# >> D, [2016-04-29T11:10:35.907203 #94256] DEBUG -- : (0.0ms) begin transaction
# >> D, [2016-04-29T11:10:35.907545 #94256] DEBUG -- : SQL (0.0ms) INSERT INTO "teams" ("name") VALUES (?) [["name", "Cubs"]]
# >> D, [2016-04-29T11:10:35.907758 #94256] DEBUG -- : (0.0ms) commit transaction
# >> D, [2016-04-29T11:10:35.936549 #94256] DEBUG -- : (0.1ms) begin transaction
# >> D, [2016-04-29T11:10:35.937435 #94256] DEBUG -- : SQL (0.1ms) INSERT INTO "fans" ("name") VALUES (?) [["name", "Jim"]]
# >> D, [2016-04-29T11:10:35.938180 #94256] DEBUG -- : SQL (0.1ms) INSERT INTO "fans_teams" ("team_id", "fan_id") VALUES (?, ?) [["team_id", 1], ["fan_id", 1]]
# >> D, [2016-04-29T11:10:35.938819 #94256] DEBUG -- : SQL (0.0ms) INSERT INTO "fans_teams" ("team_id", "fan_id") VALUES (?, ?) [["team_id", 2], ["fan_id", 1]]
# >> D, [2016-04-29T11:10:35.939675 #94256] DEBUG -- : (0.0ms) commit transaction
# >> D, [2016-04-29T11:10:35.940784 #94256] DEBUG -- : (0.0ms) begin transaction
# >> D, [2016-04-29T11:10:35.941198 #94256] DEBUG -- : SQL (0.0ms) INSERT INTO "fans" ("name") VALUES (?) [["name", "Sally"]]
# >> D, [2016-04-29T11:10:35.941676 #94256] DEBUG -- : SQL (0.0ms) INSERT INTO "fans_teams" ("team_id", "fan_id") VALUES (?, ?) [["team_id", 1], ["fan_id", 2]]
# >> D, [2016-04-29T11:10:35.942121 #94256] DEBUG -- : (0.0ms) commit transaction
# >> D, [2016-04-29T11:10:35.942967 #94256] DEBUG -- : (0.0ms) begin transaction
# >> D, [2016-04-29T11:10:35.943359 #94256] DEBUG -- : SQL (0.0ms) INSERT INTO "fans" ("name") VALUES (?) [["name", "Charlie"]]
# >> D, [2016-04-29T11:10:35.943835 #94256] DEBUG -- : SQL (0.0ms) INSERT INTO "fans_teams" ("team_id", "fan_id") VALUES (?, ?) [["team_id", 2], ["fan_id", 3]]
# >> D, [2016-04-29T11:10:35.944287 #94256] DEBUG -- : (0.0ms) commit transaction
# >> D, [2016-04-29T11:10:35.945192 #94256] DEBUG -- : Team Load (0.1ms) SELECT "teams".* FROM "teams" ORDER BY "teams"."id" ASC LIMIT 1
# >> D, [2016-04-29T11:10:35.956820 #94256] DEBUG -- : Fan Load (0.1ms) SELECT "fans".* FROM "fans" INNER JOIN "fans_teams" ON "fans"."id" = "fans_teams"."fan_id" WHERE "fans_teams"."team_id" = ? [["team_id", 1]]
# >> D, [2016-04-29T11:10:35.958233 #94256] DEBUG -- : Team Load (0.1ms) SELECT "teams".* FROM "teams" INNER JOIN "fans_teams" ON "teams"."id" = "fans_teams"."team_id" WHERE "fans_teams"."fan_id" = ? [["fan_id", 2]]
# >> D, [2016-04-29T11:10:35.958912 #94256] DEBUG -- : Fan Load (0.1ms) SELECT "fans".* FROM "fans" INNER JOIN "fans_teams" ON "fans"."id" = "fans_teams"."fan_id" WHERE "fans_teams"."team_id" = ? [["team_id", 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment