diff --git a/.github/workflows/mariadb.yml b/.github/workflows/mariadb.yml index f1ff77d0..565bda7e 100644 --- a/.github/workflows/mariadb.yml +++ b/.github/workflows/mariadb.yml @@ -21,7 +21,7 @@ jobs: MYSQL_PASSWORD: github MYSQL_DATABASE: composite_primary_keys_unittest options: >- - --health-cmd "mysqladmin ping -h localhost" + --health-cmd "healthcheck.sh --connect --innodb_initialized" --health-interval 10s --health-timeout 5s --health-retries 5 diff --git a/lib/composite_primary_keys/associations/association.rb b/lib/composite_primary_keys/associations/association.rb index 5b7c78eb..1a182bb0 100644 --- a/lib/composite_primary_keys/associations/association.rb +++ b/lib/composite_primary_keys/associations/association.rb @@ -11,8 +11,8 @@ def creation_attributes attributes[key1] = owner[key2] end - if reflection.options[:as] - attributes[reflection.type] = owner.class.base_class.name + if reflection.type + attributes[reflection.type] = owner.class.polymorphic_name end end diff --git a/lib/composite_primary_keys/associations/association_scope.rb b/lib/composite_primary_keys/associations/association_scope.rb index 3389ca37..98be8686 100644 --- a/lib/composite_primary_keys/associations/association_scope.rb +++ b/lib/composite_primary_keys/associations/association_scope.rb @@ -64,4 +64,4 @@ def next_chain_scope(scope, reflection, next_reflection) end end end -end \ No newline at end of file +end diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml index 36cb0b14..0cffdbc1 100644 --- a/test/fixtures/comments.yml +++ b/test/fixtures/comments.yml @@ -15,3 +15,9 @@ user_comment_2: article: second person_id: 2 person_type: User + +user_comment_3: + id: 4 + article: first + person_id: 1 + person_type: User1 diff --git a/test/fixtures/user_with_polymorphic_name.rb b/test/fixtures/user_with_polymorphic_name.rb new file mode 100644 index 00000000..bfef180f --- /dev/null +++ b/test/fixtures/user_with_polymorphic_name.rb @@ -0,0 +1,9 @@ +class UserWithPolymorphicName < ActiveRecord::Base + self.table_name = "users" + + has_many :comments, :as => :person + + def self.polymorphic_name + "User1" + end +end diff --git a/test/test_polymorphic.rb b/test/test_polymorphic.rb index db36491a..1f43186b 100644 --- a/test/test_polymorphic.rb +++ b/test/test_polymorphic.rb @@ -40,4 +40,10 @@ def test_clear_has_many_through article.user_commentators = [] assert_equal(0, article.comments.size) end + + def test_polymorphic_has_many_with_polymorphic_name + comments = UserWithPolymorphicName.find(1).comments + assert_equal 1, comments[0].person_id + assert_equal "User1", comments[0].person_type + end end