Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Last active December 7, 2020 09:30
Show Gist options
  • Save benoittgt/852aa00e27914e059ec70f06de68bad4 to your computer and use it in GitHub Desktop.
Save benoittgt/852aa00e27914e059ec70f06de68bad4 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "6.0.3"
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 :user_invitations, force: true do |t|
t.integer :user_id, null: false
end
create_table :sms_messages do |t|
t.string :twilio_id
t.bigint :sms_messageable_id
t.string :sms_messageable_type
t.timestamps
end
add_index :sms_messages, [:sms_messageable_type, :sms_messageable_id], name: 'sms_messages_polymorph_idx'
end
class UserInvitation < ActiveRecord::Base
has_one :sms_message, as: :sms_messageable
end
class SmsMessage < ActiveRecord::Base
belongs_to :sms_messageable, polymorphic: true
def user_invitation=(user_invitation)
user_invitation.sms_message = self
end
def user_invitation
UserInvitation.find_by!(sms_message: self)
end
end
class BugTest < Minitest::Test
def test_association_stuff
user_invitation = UserInvitation.create(user_id: 11345)
sms_message = SmsMessage.create
sms_message.user_invitation = user_invitation
assert_equal user_invitation, SmsMessage.last.user_invitation
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment