【问题标题】:Multiple many to one associations多个多对一关联
【发布时间】:2012-10-15 18:33:14
【问题描述】:

在我的应用中,每个游戏都涉及两个角色不同的玩家。一个玩猫,一个玩狗。如何在 Ruby 的数据映射器中描述这一点?

文档仅提供属性名称与类名称匹配的示例,这将我们限制为每个类一个关联http://datamapper.org/docs/associations.html

我希望我的游戏有一个猫玩家和一个狗玩家。

【问题讨论】:

    标签: datamapper ruby-datamapper


    【解决方案1】:

    您链接的文档有答案。仔细阅读。

    class Player
      include DataMapper::Resource
    end
    
    class Game
      include DataMapper::Resource
      belongs_to :cat, 'Player'
      belongs_to :dog, 'Player'
    end
    

    更新:如果需要,您可以在 Player 模型中使用这些关联

    class Player
      include DataMapper::Resource
      has n, :cat_games, :child_key  => [ :cat_id ]
      has n, :dog_games, :child_key  => [ :dog_id ]
    end
    

    【讨论】:

    • 甜蜜!我应该以某种方式在 Player 类中提及关联吗?
    • 仅当您愿意时。更新了我的答案。
    • 再次感谢。非常喜欢使用 Data Mapper。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 2015-01-26
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多