has_many的源反射错误:through [英] Source Reflection Errors with has_many :through

查看:94
本文介绍了has_many的源反射错误:through的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个系统,我的网站的用户可以在其中收藏页面.这些页面有两种类型,即俱乐部或体育.因此,我有四个模型,与之相关:

I'm attempting to create a system where my site's users can favorites pages. Those pages have two types, either clubs or sports. So, I have four models, associated as such:

用户模型:

class User < ActiveRecord::Base
    ..
    has_many :favorites
    has_many :sports,    :through => :favorites
    has_many :clubs,     :through => :favorites
    ..
end

收藏夹模型:

class Favorite < ActiveRecord::Base
    ..

    belongs_to :user
    belongs_to :favoritable, :polymorphic => true

end

俱乐部型号:

class Club < ActiveRecord::Base
    ..

    has_many :favorites, :as => :favoritable
    has_many :users, :through => :favorites

    def to_param
      slug
    end
end

运动模型:

class Sport < ActiveRecord::Base
    ..

    def to_param
        slug
    end

    ..

    has_many :favorites,   :as => :favoritable
    has_many :users,       :through => :favorites

    ..
end

从本质上讲,用户通过收藏夹拥有许多运动或俱乐部,并且收藏夹,运动和俱乐部之间的关联是多态的.

Essentially, the User has_many sports or clubs through favorites, and the association between favorites, sports, and clubs is polymorphic.

实际上,这一切都完全按照我想要的方式工作,并且我设计的整个系统都可以工作.但是,我在网站上使用Rails_Admin,但在三个地方出现错误:

In practice, this is all working exactly the way I want it to, and the whole system I have designed works. However, I'm using Rails_Admin on my site, and I get an error in three places:

  1. 首次加载仪表板(/admin)时.如果我刷新页面,效果很好.
  2. 在Rails_Admin中加载用户模型时
  3. 在Rails_Admin中加载收藏夹模型时

这是/admin/user (要点)上的错误消息.所有错误都相似,引用ActiveRecord::Reflection::ThroughReflection#foreign_key delegated to source_reflection.foreign_key, but source_reflection is nil:.

Here is the error message on /admin/user (gist). All of the errors are similar, referencing ActiveRecord::Reflection::ThroughReflection#foreign_key delegated to source_reflection.foreign_key, but source_reflection is nil:.

有人能指出我正确的方向,以便我解决此问题吗?我到处搜索,并询问其他程序员/专业人士,但没人能在我的模型中发现错误.非常感谢!

Can anyone point me in the right direction so that I can fix this? I've searched all over, and asked other programmers/professionals, but no one could spot the error in my models. Thanks so much!

推荐答案

好吧,我终于解决了这个问题,并认为我会发布此修复程序,以防将来对其他人有帮助(没有人)喜欢找到遇到相同问题且没有答案的人.

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).

事实证明,对于多态的has_many :through,还需要更多配置.我的用户模型应该看起来像这样:

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

这个答案关于多态has_many :through关联的另一个问题是什么帮助我弄清了这一点.

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

这篇关于has_many的源反射错误:through的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆