{"action":"created","issue":{"url":"https://api.github.com/repos/nviennot/nobrainer/issues/114","labels_url":"https://api.github.com/repos/nviennot/nobrainer/issues/114/labels{/name}","comments_url":"https://api.github.com/repos/nviennot/nobrainer/issues/114/comments","events_url":"https://api.github.com/repos/nviennot/nobrainer/issues/114/events","html_url":"https://github.com/nviennot/nobrainer/issues/114","id":53766447,"number":114,"title":"Reflection: Find out whether a through-association is polymorphic","user":{"login":"mbrock","id":8154,"avatar_url":"https://avatars.githubusercontent.com/u/8154?v=3","gravatar_id":"","url":"https://api.github.com/users/mbrock","html_url":"https://github.com/mbrock","followers_url":"https://api.github.com/users/mbrock/followers","following_url":"https://api.github.com/users/mbrock/following{/other_user}","gists_url":"https://api.github.com/users/mbrock/gists{/gist_id}","starred_url":"https://api.github.com/users/mbrock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbrock/subscriptions","organizations_url":"https://api.github.com/users/mbrock/orgs","repos_url":"https://api.github.com/users/mbrock/repos","events_url":"https://api.github.com/users/mbrock/events{/privacy}","received_events_url":"https://api.github.com/users/mbrock/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-01-08T16:02:22Z","updated_at":"2015-01-08T16:19:17Z","closed_at":null,"body":"While working on porting `rails_admin` to NoBrainer, I stumbled across the need to determine whether an association is polymorphic.\r\n\r\nFor regular non-through associations, I simply do `association.target_model.is_polymorphic`.\r\n\r\nHowever, a through-association doesn't have a `target_model`, but instead a `target_association`.\r\n\r\nIt's not clear to me how I would find the actual targetted model through this.\r\n\r\nIn the example I'm working with—a HABTM-like setup—I have:\r\n\r\n```ruby\r\nclass Team\r\n  include NoBrainer::Document\r\n  has_many :team_fans\r\n  has_many :fans, through: :team_fans\r\nend\r\n\r\nclass Fan\r\n  include NoBrainer::Document\r\n  has_many :team_fans\r\n  has_many :teams, through: :team_fans\r\nend\r\n\r\nclass TeamFan\r\n  include NoBrainer::Document\r\n  belongs_to :team\r\n  belongs_to :fan\r\nend\r\n```\r\n\r\nTo give a clearer idea of what I'm trying to do, here's what I'm looking at in `irb`:\r\n\r\n```\r\n(byebug) association\r\n#<NoBrainer::Document::Association::HasManyThrough::Metadata:0x00000002eec968 @owner_model=Team, @target_name=:fans, @options={:through=>:team_fans}>\r\n(byebug) association.target_model\r\nNoMethodError Exception: undefined method `target_model' for #<NoBrainer::Document::Association::HasManyThrough::Metadata:0x00000002eec968>\r\nnil\r\n(byebug) association.through_associations\r\nNoMethodError Exception: undefined method `through_associations' for #<NoBrainer::Document::Association::HasManyThrough::Metadata:0x00000002eec968>\r\nnil\r\n(byebug) association.through_association\r\n#<NoBrainer::Document::Association::HasMany::Metadata:0x00000002ecc730 @owner_model=Team, @target_name=:team_fans, @options={}>\r\n(byebug) association.through_association.target_model\r\nTeamFan\r\n```\r\n\r\nI'm looking around for a way to access the `Fan` model. Maybe I'm missing something obvious? :)"},"comment":{"url":"https://api.github.com/repos/nviennot/nobrainer/issues/comments/69203405","html_url":"https://github.com/nviennot/nobrainer/issues/114#issuecomment-69203405","issue_url":"https://api.github.com/repos/nviennot/nobrainer/issues/114","id":69203405,"user":{"login":"nviennot","id":297060,"avatar_url":"https://avatars.githubusercontent.com/u/297060?v=3","gravatar_id":"","url":"https://api.github.com/users/nviennot","html_url":"https://github.com/nviennot","followers_url":"https://api.github.com/users/nviennot/followers","following_url":"https://api.github.com/users/nviennot/following{/other_user}","gists_url":"https://api.github.com/users/nviennot/gists{/gist_id}","starred_url":"https://api.github.com/users/nviennot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nviennot/subscriptions","organizations_url":"https://api.github.com/users/nviennot/orgs","repos_url":"https://api.github.com/users/nviennot/repos","events_url":"https://api.github.com/users/nviennot/events{/privacy}","received_events_url":"https://api.github.com/users/nviennot/received_events","type":"User","site_admin":false},"created_at":"2015-01-08T16:19:17Z","updated_at":"2015-01-08T16:19:17Z","body":"So you want to know the target_model of a has many through association if I understand correctly?\r\n\r\nIf so, you are not missing anything, there is no code traverse the associations like that.\r\nThe only code that traverses recursively the associations is the eager loading logic, which the has many through leverages to do its magic.\r\n\r\nLet me add a bit of code for that."}}