Skip to content

Instantly share code, notes, and snippets.

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/53ec901021ad07ca9b18 to your computer and use it in GitHub Desktop.
Save JoshCheek/53ec901021ad07ca9b18 to your computer and use it in GitHub Desktop.
Active Record self join table with mismatched names
require 'stringio'
def capture_stdout
real_stdout = $stdout.dup
read, write = IO.pipe
$stdout.reopen write
begin
yield
ensure
$stdout.reopen real_stdout
end
write.close
read.read
end
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
create_table :users do |t|
t.string :name
end
create_table :artists_to_supporters do |t|
t.integer :artist_id
t.integer :supporter_id
end
end
class User < ActiveRecord::Base
has_many :artist_to_supporters_i_am_the_artist , foreign_key: :artist_id, class_name: 'ArtistToSupporter'
has_many :artist_to_supporters_i_am_the_supporter, foreign_key: :supporter_id, class_name: 'ArtistToSupporter'
has_many :supporters, through: :artist_to_supporters_i_am_the_artist
has_many :supported_artists, through: :artist_to_supporters_i_am_the_supporter, source: :artist
end
class ArtistToSupporter < ActiveRecord::Base
self.table_name = :artists_to_supporters
belongs_to :artist, class_name: 'User'
belongs_to :supporter, class_name: 'User'
end
josh = User.create! name: 'Josh'
jai = User.create! name: 'Jai', supporters: [josh]
anh = User.create! name: 'Anh', supporters: [josh, jai]
anh # => #<User id: 3, name: "Anh">
.supporters # => #<ActiveRecord::Associations::CollectionProxy [#<User id: 1, name: "Josh">, #<User id: 2, name: "Jai">]>
.first # => #<User id: 1, name: "Josh">
.supported_artists # => #<ActiveRecord::Associations::CollectionProxy [#<User id: 2, name: "Jai">, #<User id: 3, name: "Anh">]>
.last # => #<User id: 3, name: "Anh">
.supporters # => #<ActiveRecord::Associations::CollectionProxy [#<User id: 1, name: "Josh">, #<User id: 2, name: "Jai">]>
.first # => #<User id: 1, name: "Josh">
.supported_artists # => #<ActiveRecord::Associations::CollectionProxy [#<User id: 2, name: "Jai">, #<User id: 3, name: "Anh">]>
.first # => #<User id: 2, name: "Jai">
# >> D, [2015-11-22T17:06:52.572633 #30817] DEBUG -- : (1.7ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar)
# >> D, [2015-11-22T17:06:52.574366 #30817] DEBUG -- : (0.4ms) CREATE TABLE "artists_to_supporters" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "artist_id" integer, "supporter_id" integer)
# >> D, [2015-11-22T17:06:52.668340 #30817] DEBUG -- : (0.2ms) begin transaction
# >> D, [2015-11-22T17:06:52.683719 #30817] DEBUG -- : SQL (0.3ms) INSERT INTO "users" ("name") VALUES (?) [["name", "Josh"]]
# >> D, [2015-11-22T17:06:52.684836 #30817] DEBUG -- : (0.2ms) commit transaction
# >> D, [2015-11-22T17:06:52.733617 #30817] DEBUG -- : (0.2ms) begin transaction
# >> D, [2015-11-22T17:06:52.735657 #30817] DEBUG -- : SQL (0.2ms) INSERT INTO "users" ("name") VALUES (?) [["name", "Jai"]]
# >> D, [2015-11-22T17:06:52.738052 #30817] DEBUG -- : SQL (0.2ms) INSERT INTO "artists_to_supporters" ("supporter_id", "artist_id") VALUES (?, ?) [["supporter_id", 1], ["artist_id", 2]]
# >> D, [2015-11-22T17:06:52.739779 #30817] DEBUG -- : (0.1ms) commit transaction
# >> D, [2015-11-22T17:06:52.744387 #30817] DEBUG -- : (0.2ms) begin transaction
# >> D, [2015-11-22T17:06:52.746182 #30817] DEBUG -- : SQL (0.2ms) INSERT INTO "users" ("name") VALUES (?) [["name", "Anh"]]
# >> D, [2015-11-22T17:06:52.747994 #30817] DEBUG -- : SQL (0.1ms) INSERT INTO "artists_to_supporters" ("supporter_id", "artist_id") VALUES (?, ?) [["supporter_id", 1], ["artist_id", 3]]
# >> D, [2015-11-22T17:06:52.749935 #30817] DEBUG -- : SQL (0.1ms) INSERT INTO "artists_to_supporters" ("supporter_id", "artist_id") VALUES (?, ?) [["supporter_id", 2], ["artist_id", 3]]
# >> D, [2015-11-22T17:06:52.752345 #30817] DEBUG -- : (0.1ms) commit transaction
# >> D, [2015-11-22T17:06:52.775518 #30817] DEBUG -- : User Load (0.4ms) SELECT "users".* FROM "users" INNER JOIN "artists_to_supporters" ON "users"."id" = "artists_to_supporters"."artist_id" WHERE "artists_to_supporters"."supporter_id" = ? [["supporter_id", 1]]
# >> D, [2015-11-22T17:06:52.779512 #30817] DEBUG -- : User Load (0.3ms) SELECT "users".* FROM "users" INNER JOIN "artists_to_supporters" ON "users"."id" = "artists_to_supporters"."supporter_id" WHERE "artists_to_supporters"."artist_id" = ? [["artist_id", 3]]
# >> D, [2015-11-22T17:06:52.781708 #30817] DEBUG -- : User Load (0.1ms) SELECT "users".* FROM "users" INNER JOIN "artists_to_supporters" ON "users"."id" = "artists_to_supporters"."artist_id" WHERE "artists_to_supporters"."supporter_id" = ? [["supporter_id", 1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment