如何拥有多个相同类型的唯一 has_one 关联?
我正在制作一个 ruby 应用程序来跟踪乒乓球比赛统计数据。这就是我的播放器模型到目前为止的样子,
class Game < ActiveRecord::Base
#has one winner
has_one :winner,
:source => :user
#has one loser
has_one :loser,
:source => :user
belongs_to :player
alias :recorded_by :player
end
但是当我加载页面时,我收到一条错误消息“未知密钥:源”。如果您无法判断我在做什么,则该模型应该包含两个用户对象,一个标记为“获胜者”,一个标记为“失败者”。谁能指出我在这里做错了什么?
I am making a ruby app that will track ping pong game stats. This is what the model for my player looks like so far
class Game < ActiveRecord::Base
#has one winner
has_one :winner,
:source => :user
#has one loser
has_one :loser,
:source => :user
belongs_to :player
alias :recorded_by :player
end
When I load the page though, I get an error saying "unknown key: source." If you cannot tell what I am doing, the model is supposed to contain two user objects, one labeled "winner" and one labeled "loser." Can anyone point out what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
has_one :loser, :class_name => 'User'
这是必需的,因为rails 无法猜测loser_id 的正确模型:source
另一方面用于 :through 关系。try
has_one :loser, :class_name => 'User'
this is needed because rails can't guess the correct model for loser_id:source
on the other hand is used in :through relations.