Skip to content

Instantly share code, notes, and snippets.

@neerajsingh0101
Created May 22, 2013 23:46
Show Gist options
  • Save neerajsingh0101/5631821 to your computer and use it in GitHub Desktop.
Save neerajsingh0101/5631821 to your computer and use it in GitHub Desktop.
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'sqlite3'
gem 'debugger'
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 :accounts do |t|
end
create_table :permissions do |t|
t.references :accessible, polymorphic: true
t.references :permissible, polymorphic: true
end
create_table :users do |t|
end
end
class Account < ActiveRecord::Base
has_many :permissions, as: :permissible
has_many :users, through: :permissions, source: :accessible, source_type: 'User'
end
class Permission < ActiveRecord::Base
belongs_to :accessible, polymorphic: true
belongs_to :permissible, polymorphic: true
end
class User < ActiveRecord::Base
has_many :permissions, as: :accessible
has_many :accounts, through: :permissions, source: :permissible, source_type: 'Account'
validates_presence_of :accounts
end
class BugTest < MiniTest::Unit::TestCase
def test_association_stuff
User.delete_all
Account.delete_all
account = Account.create!
account.users.create!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment