Skip to content

Instantly share code, notes, and snippets.

@vcsjones
Created June 18, 2018 23:16
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 vcsjones/9f3b53d8fc061a0afaf4822dcfe5a5b2 to your computer and use it in GitHub Desktop.
Save vcsjones/9f3b53d8fc061a0afaf4822dcfe5a5b2 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", "=5.2.0"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :guilds do |t|
t.string :name, null: false
end
create_table :authors do |t|
t.string :name, null: false
t.datetime :deleted_at
t.references :guild
end
create_table :books do |t|
t.references :author
t.string :title, null: false
end
execute <<-SQL
CREATE VIEW v_books AS
SELECT * FROM books
SQL
end
class Author < ActiveRecord::Base
has_many :books
belongs_to :guild
end
class Book < ActiveRecord::Base
self.table_name = "v_books"
belongs_to :author
def readonly?
true
end
def persisted?
true
end
end
class Guild < ActiveRecord::Base
has_many :authors
end
class BugTest < Minitest::Test
def test_view
guild = Guild.create!(name: "Authors Guild")
author = Author.create!(name: "Stephen King", guild: guild)
query = <<-SQL
INSERT INTO books(author_id, title) VALUES (#{author.id}, 'The Dark Tower')
SQL
ActiveRecord::Base.connection.execute query
books = Author.joins(:guild).includes(:guild, :books).find(author.id).books.to_a
assert_equal 1, books.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment