Source Reflection Errors with has_many :through

我的梦境 提交于 2019-12-05 13:20:24
Alex LaFroscia

Alright, well, I finally worked this out, and figured that I'd post the fix just in case it helps someone else out in the future (no one likes finding someone else with the same problem and no posted answer).

As it turns out, with a polymorphic has_many :through, there is a little more configuration needed. My User model should have looked like this:

class User < ActiveRecord::Base
    ..
    has_many :favorites
    has_many :sports, :through => :favorites, :source => :favoritable, :source_type => "Sport"
    has_many :clubs,  :through => :favorites, :source => :favoritable, :source_type => "Club"
    ..
end

This answer to another question about polymorphic has_many :through associations is what helped me figure this out.

I encountered this error when the code included a has_many for an association that doesn't exist (mid-refactor). So it can also be caused by some general has_many misconfigure. The Ruby/Rails code never cares because the dynamic style of Ruby means the association is only called on demand. But Rails-Admin exhaustively inspects properties, leading to reflection problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!