Created
June 18, 2018 23:16
-
-
Save vcsjones/9f3b53d8fc061a0afaf4822dcfe5a5b2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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