Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'sprockets', github: 'rails/sprockets'
gem 'sprockets-rails', github: 'rails/sprockets-rails'
gem 'sass-rails', github: 'rails/sass-rails'
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 :accounts, force: true do |t|
t.integer :firm_id
t.string :firm_name
t.integer :credit_limit
end
create_table :companies, force: true do |t|
t.string :type
t.integer :firm_id
t.string :firm_name
t.string :name
t.integer :client_of
t.integer :rating, default: 1
t.integer :account_id
t.string :description, default: ""
end
add_index :companies, [:firm_id, :type, :rating], name: "company_index"
add_index :companies, [:firm_id, :type], name: "company_partial_index", where: "rating > 10"
add_index :companies, :name, name: 'company_name_index', using: :btree
end
class AbstractCompany < ActiveRecord::Base
self.abstract_class = true
end
class Company < ActiveRecord::Base
scope :of_first_firm, lambda {
joins(:account => :firm).
where('firms.id' => 1)
}
has_many :accounts
end
class Firm < Company
has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true
has_many :accounts
end
class InheritanceTest < ActiveSupport::TestCase
def test_scope_inherited_properly
assert_nothing_raised { Company.of_first_firm.to_sql }
# assert_nothing_raised { Client.of_first_firm }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment