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 mtodd/6e40adff3ba0459c34a3 to your computer and use it in GitHub Desktop.
Save mtodd/6e40adff3ba0459c34a3 to your computer and use it in GitHub Desktop.
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