Rails 3建模复杂Has_Many关系

时间:2012-08-28 15:40:39

标签: ruby-on-rails ruby-on-rails-3 activerecord

我正在为游戏构建一种留言板系统,而​​且我在建立关系时遇到了问题。这是我到目前为止所做的:

Models
------
User (Has Many Posts)
Post (Belongs to User, Belongs to Game)
Game (Has Many Posts)

这是我需要添加的内容。

Models
------
Server (Belongs to Game but also part of Post)
Replies (Belongs to Post and Post's User but needs to function like private messaging)

对这两件事进行建模的最简单方法是什么?我对服务器的最佳猜测是:

Server - Has Many Posts, Belongs To Game
Posts - Belongs To Server
Game - Has Many Servers

我不知道从哪里开始回复。帖子和用户将有很多回复,但回复将有两个用户和一个帖子。感谢。

1 个答案:

答案 0 :(得分:0)

我用这些关系解决了这个问题:

模型服务器

belongs_to :game
has_many :posts

模特游戏

has_many :posts
has_many :servers

模特回复

belongs_to :post
belongs_to :poster, class_name: :user, foreign_key: :poster_id
belongs_to :replier, class_name: :user, foreign_key: :replier_id

模特职位

belongs_to :user
belongs_to :game
belongs_to :server
has_many :replies, dependent: :destroy

模型用户

has_many :posts, dependent: :destroy
has_many :replies, dependent: :destroy