Last active
June 2, 2021 02:37
-
-
Save jinhucheung/ab5598349da8aacbb5fd5cd335194511 to your computer and use it in GitHub Desktop.
polymorphic_primary_key_alias.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem "rails", github: "rails/rails", branch: "main" | |
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 :users, force: true do |t| | |
end | |
create_table :profiles, force: true do |t| | |
t.string :user_id | |
t.string :user_type | |
end | |
end | |
class User < ActiveRecord::Base | |
alias_attribute :primary_key_for_profile, :id | |
has_one :profile | |
end | |
class Profile < ActiveRecord::Base | |
belongs_to :user, polymorphic: true, primary_key: :primary_key_for_profile | |
end | |
class BugTest < Minitest::Test | |
def test_profile | |
user = User.create! | |
profile = Profile.create!(user_type: 'User', user_id: user.id) | |
assert_match(user.id.to_s, Profile.where(user: user).to_sql) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem "rails", github: "rails/rails", branch: "main" | |
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 :users, force: true do |t| | |
end | |
create_table :profiles, force: true do |t| | |
t.string :user_id | |
t.string :user_type | |
end | |
end | |
class User < ActiveRecord::Base | |
alias_attribute :primary_key_for_profile, :id | |
has_one :profile | |
end | |
class Profile < ActiveRecord::Base | |
belongs_to :user, primary_key: :primary_key_for_profile | |
end | |
class BugTest < Minitest::Test | |
def test_profile | |
user = User.create! | |
profile = Profile.create!(user_type: 'User', user_id: user.id) | |
assert_match(user.id.to_s, Profile.where(user: user).to_sql) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment