Skip to content

Commit

Permalink
should still able to set other reflection successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
wendy-clio committed Dec 19, 2023
1 parent 148a2cc commit ea41c2c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module PolymorphicForeignAssociationExtension

def set_owner_attributes(record)
super
if reflection.foreign_integer_type && reflection.integer_type
if reflection.try(:foreign_integer_type) && reflection.try(:integer_type)
record._write_attribute(reflection.foreign_integer_type, reflection.integer_type)
end
end
Expand Down
9 changes: 9 additions & 0 deletions spec/polymorphic_integer_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,13 @@ class InlineDrink2 < ActiveRecord::Base
expect(link.normal_target_type).to eq("InlineDrink2")
end
end

context "when using other reflection" do
it "owner able to set up ActiveRecord::Reflection::ThroughReflection successfully" do
profile_history = ProfileHistory.new
owner.profile_histories << profile_history

expect(owner.profile_histories).to eq([profile_history])
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
require 'support/person'
require 'support/food'
require 'support/drink'
require 'support/profile'
require 'support/profile_history'
require 'support/namespaced_activity'
require 'byebug'
require 'pry'
Expand Down
16 changes: 16 additions & 0 deletions spec/support/migrations/8_create_profile_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateProfileTable < ActiveRecord::Migration[5.0]

def up
create_table :profiles do |t|
t.integer :person_id
t.integer :profile_history_id
end
end

def down
drop_table :profiles
end

end


14 changes: 14 additions & 0 deletions spec/support/migrations/9_create_profile_history_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateProfileHistoryTable < ActiveRecord::Migration[5.0]

def up
create_table :profile_histories do |t|
end
end

def down
drop_table :profile_histories
end

end


2 changes: 2 additions & 0 deletions spec/support/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class Person < ActiveRecord::Base
has_many :source_links, as: :source, integer_type: true, class_name: "Link"

has_many :pet_source_links, class_name: "Link", through: :pets, source: :source_links
has_many :profiles
has_many :profile_histories, class_name: "ProfileHistory", through: :profiles
end
4 changes: 4 additions & 0 deletions spec/support/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Profile < ActiveRecord::Base
belongs_to :person
belongs_to :profile_history
end
3 changes: 3 additions & 0 deletions spec/support/profile_history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ProfileHistory < ActiveRecord::Base
has_many :profiles
end

0 comments on commit ea41c2c

Please sign in to comment.