{"action":"closed","issue":{"url":"https://api.github.com/repos/rails/rails/issues/23209","labels_url":"https://api.github.com/repos/rails/rails/issues/23209/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/23209/comments","events_url":"https://api.github.com/repos/rails/rails/issues/23209/events","html_url":"https://github.com/rails/rails/issues/23209","id":128324482,"number":23209,"title":"Has many through polymorphic source inserts from source_type","user":{"login":"heaven","id":126732,"avatar_url":"https://avatars.githubusercontent.com/u/126732?v=3","gravatar_id":"","url":"https://api.github.com/users/heaven","html_url":"https://github.com/heaven","followers_url":"https://api.github.com/users/heaven/followers","following_url":"https://api.github.com/users/heaven/following{/other_user}","gists_url":"https://api.github.com/users/heaven/gists{/gist_id}","starred_url":"https://api.github.com/users/heaven/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/heaven/subscriptions","organizations_url":"https://api.github.com/users/heaven/orgs","repos_url":"https://api.github.com/users/heaven/repos","events_url":"https://api.github.com/users/heaven/events{/privacy}","received_events_url":"https://api.github.com/users/heaven/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"},{"url":"https://api.github.com/repos/rails/rails/labels/With%20reproduction%20steps","name":"With reproduction steps","color":"009800"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-01-23T12:08:23Z","updated_at":"2016-01-24T18:08:32Z","closed_at":"2016-01-24T18:08:32Z","body":"I have the next STI:\r\n```ruby\r\nclass ListItem < ActiveRecord::Base; end\r\nclass Feeling < ListItem; end\r\n```\r\n\r\nThe :through association:\r\n```ruby\r\nclass ListItemRelationship < ActiveRecord::Base\r\n  belongs_to :list_item, :polymorphic => true\r\n  belongs_to :listable, :polymorphic => true\r\nend\r\n```\r\n\r\nAnd the next model where I need to add a `has_many` association:\r\n```ruby\r\nclass StressTest < ActiveRecord::Base\r\n  has_many :list_item_relationships, :as => :listable\r\n  has_many :feelings, :through => :list_item_relationships, :source => :list_item, :source_type => \"Feeling\"\r\nend\r\n```\r\n\r\nThe key think here is :source_type, which allows this to work. When I do:\r\n```ruby\r\ntest = StressTest.create\r\ntest.feelings << Feeling.first\r\n```\r\n\r\nMakes the following wrong insertion (even though the source_type specifies \"Feeling\", it inserts \"ListItem\"):\r\n```sql\r\nINSERT INTO \"list_item_relationships\" (\"listable_type\", \"list_item_type\", \"listable_id\", \"list_item_id\", \"created_at\", \"updated_at\") VALUES ($1, $2, $3, $4, $5, $6) RETURNING \"id\"  [[\"listable_type\", \"StressTest\"], [\"list_item_type\", \"ListItem\"], [\"listable_id\", 3], [\"list_item_id\", 8], [\"created_at\", \"2016-01-23 11:58:34.079597\"], [\"updated_at\", \"2016-01-23 11:58:34.079597\"]]\r\n```\r\n\r\nSo then:\r\n```ruby\r\ntest.feelings.size\r\n# => 1\r\ntest.feelings.count\r\n# => 0\r\ntest.list_item_relationships.count\r\n# => 1\r\n```\r\n\r\nWhen I call `#count` it looks for records with `list_item_type: Feeling`, when I add records to the association it inserts `list_item_type: ListItem`.\r\n\r\nSame happens when I do `test.feeling_ids = [1, 2, 3, ...]`, it always inserts `list_item_type` of parent class of my STI.\r\n\r\nRails 4.2.5"}}