【发布时间】:2013-05-21 19:11:59
【问题描述】:
我正在尝试创建一个系统,让我的网站的用户可以收藏页面。这些页面有两种类型,俱乐部或运动。所以,我有四个模型,关联如下:
用户模型:
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
本质上,用户通过收藏有_many运动或俱乐部,收藏、运动和俱乐部之间的关联是多态的。
在实践中,这一切都完全按照我想要的方式运行,并且我设计的整个系统都可以正常工作。但是,我在我的网站上使用 Rails_Admin,我在三个地方收到错误:
- 第一次加载仪表板 (/admin) 时。如果我刷新页面,它工作正常。
- 在 Rails_Admin 中加载用户模型时
- 在 Rails_Admin 中加载收藏夹模型时
这是/admin/user (gist) 上的错误消息。所有错误都是相似的,引用ActiveRecord::Reflection::ThroughReflection#foreign_key delegated to source_reflection.foreign_key, but source_reflection is nil:。
谁能指出我正确的方向,以便我可以解决这个问题?我到处搜索,并询问了其他程序员/专业人士,但没有人能在我的模型中发现错误。非常感谢!
【问题讨论】:
标签: ruby-on-rails model polymorphic-associations model-associations rails-admin