Skip to content

Instantly share code, notes, and snippets.

@bschaeffer
Created June 10, 2014 21:53
Show Gist options
  • Select an option

  • Save bschaeffer/28ba90d32cc96bc8f9a1 to your computer and use it in GitHub Desktop.

Select an option

Save bschaeffer/28ba90d32cc96bc8f9a1 to your computer and use it in GitHub Desktop.
Rails Bug
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
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: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :read_marks do |t|
t.integer :user_id
t.integer :readable_id
t.string :readable_type
end
create_table :users do |t|
end
create_table :module_videos do |t|
end
end
class User < ActiveRecord::Base
has_many :read_marks
has_many :module_videos, through: :read_marks,
source: :readable, source_type: 'ModuleVideo'
end
class ModuleVideo < ActiveRecord::Base
has_many :read_marks, as: :readable
has_many :users, through: :read_marks
end
class ReadMark < ActiveRecord::Base
belongs_to :user
belongs_to :readable, polymorphic: true
end
class BugTest < Minitest::Test
def test_association_stuff
user = User.create
video = ModuleVideo.create
ReadMark.create(user: user, readable: video)
assert_equal user.module_videos.count, 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment