diff --git a/lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb b/lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb index 2b18d05..cd9cb87 100644 --- a/lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb +++ b/lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb @@ -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 diff --git a/spec/polymorphic_integer_type_spec.rb b/spec/polymorphic_integer_type_spec.rb index 3d56eb2..4af7d30 100644 --- a/spec/polymorphic_integer_type_spec.rb +++ b/spec/polymorphic_integer_type_spec.rb @@ -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 association 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9e7ce1f..fb6e13d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' diff --git a/spec/support/migrations/8_create_profile_table.rb b/spec/support/migrations/8_create_profile_table.rb new file mode 100644 index 0000000..97add18 --- /dev/null +++ b/spec/support/migrations/8_create_profile_table.rb @@ -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 + + diff --git a/spec/support/migrations/9_create_profile_history_table.rb b/spec/support/migrations/9_create_profile_history_table.rb new file mode 100644 index 0000000..6eb3054 --- /dev/null +++ b/spec/support/migrations/9_create_profile_history_table.rb @@ -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 + + diff --git a/spec/support/person.rb b/spec/support/person.rb index 59f5985..981a880 100644 --- a/spec/support/person.rb +++ b/spec/support/person.rb @@ -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 diff --git a/spec/support/profile.rb b/spec/support/profile.rb new file mode 100644 index 0000000..c669bd1 --- /dev/null +++ b/spec/support/profile.rb @@ -0,0 +1,4 @@ +class Profile < ActiveRecord::Base + belongs_to :person + belongs_to :profile_history +end diff --git a/spec/support/profile_history.rb b/spec/support/profile_history.rb new file mode 100644 index 0000000..bf55ee6 --- /dev/null +++ b/spec/support/profile_history.rb @@ -0,0 +1,3 @@ +class ProfileHistory < ActiveRecord::Base + has_many :profiles +end