【问题标题】:Implementing difficult model relations in Mongoid在 Mongoid 中实现困难的模型关系
【发布时间】:2013-05-31 15:23:17
【问题描述】:

如果没有has_ through,则无法在 mongoid 中实现提供的方案:

用户有很多帖子,属于很多项目,并且根据参与的一些项目,属于一些版块,可以是项目中的管理员

版块有很多项目、用户和帖子

项目有很多帖子和用户,属于很多部分,有些用户是这里的管理员(belong关系?)

帖子属于一个用户、一个项目和多个版块

怎么做?

对所有事物都使用多态关联?

我的代码:http://pastebin.com/sFzs1FD8

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 mongodb mongoid


    【解决方案1】:

    我自己做的:)

    # Models:
    class User
      has_and_belongs_to_many :projects
      has_many :posts, inverse_of: :author
    end
    
    class Project
      has_and_belongs_to_many :users
      has_many :posts, inverse_of: :project
    end
    
    class Post
      belongs_to :project, inverse_of: :posts
      belongs_to :author, class_name: 'User'
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 2015-04-24
      • 1970-01-01
      相关资源
      最近更新 更多