Skip to content

Instantly share code, notes, and snippets.

@m5rk
Last active August 29, 2015 14:05
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 m5rk/415570a52fb03f5ff041 to your computer and use it in GitHub Desktop.
Save m5rk/415570a52fb03f5ff041 to your computer and use it in GitHub Desktop.
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.3'
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'pry'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# 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 :my_notifications do |t|
t.integer :my_other_class_id
end
create_table :my_other_classes do |t|
t.string :message_type
t.string :recipient
end
end
class MyNotification < ActiveRecord::Base
belongs_to :my_other_class, :autosave => true
validates :recipient, presence: true
delegate :recipient, :recipient=, to: :my_other_class
before_create :build_my_class_if_missing
def build_my_class_if_missing
my_other_class ||= build_my_other_class(message_type: "MyMessageType")
end
end
class MyOtherClass < ActiveRecord::Base
has_many :my_notifications
end
class BugTest < Minitest::Test
def test_delegate
user = 'foo'
notification = MyNotification.create(recipient: user)
assert_equal notification.recipient, user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment