Skip to content

Instantly share code, notes, and snippets.

@simon2k

simon2k/app.rb Secret

Last active Aug 29, 2015
Embed
What would you like to do?
sample rails app for bug
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails', tag: 'v3.2.22'
gem 'pg', '~> 0.18.1'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'test')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :surveys, force: true do |t|
end
create_table :email_templates, force: true do |t|
t.string :survey_id
end
end
class Survey < ActiveRecord::Base
has_many :email_templates
end
class EmailTemplate < ActiveRecord::Base
belongs_to :survey
end
class BugTest < ActiveRecord::TestCase
def test_association_stuff
survey = Survey.create!
email_template = EmailTemplate.create!(survey_id: survey.id)
assert_equal survey.email_templates.to_a, [email_template]
end
end
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails', tag: 'v3.2.22'
gem 'pg', '~> 0.18.1'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'test')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :surveys, force: true
create_table :tfs, id: false, force: true do |t|
t.string :id
end
create_table :email_templates, force: true do |t|
t.string :object_with_email_template_id
t.string :object_with_email_template_type
end
end
class Survey < ActiveRecord::Base
has_many :email_templates, as: :object_with_email_template
end
class Tfs < ActiveRecord::Base
has_many :email_templates, as: :object_with_email_template
end
class EmailTemplate < ActiveRecord::Base
belongs_to :object_with_email_template, polymorphic: true
end
class BugTest < ActiveRecord::TestCase
def test_association_stuff
survey = Survey.create!
tfs = Tfs.create!(id: 'my_tfs')
survey_email_template = EmailTemplate.create!(object_with_email_template: survey)
assert_equal survey.email_templates.to_a, [survey_email_template]
tfs_email_template = EmailTemplate.create!(object_with_email_template: tfs)
assert_equal tfs.email_templates.to_a, [tfs_email_template]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment