我正在開發我的第一個真正的rails項目,並且正在尋找一些專業建議。MySQL和Rails中的外鍵建議
我期待在我的MySQL數據庫四個表(用戶,圖書,評論,標籤)及以下業務規則。
,用戶可以使一本書一個或多個評論,但評論只能屬於一本書。 用戶可以在一本書上放置一個或多個標籤,但標籤只能屬於一本書。
圖形,我凸輪了以下內容:(ASCII藝術永遠不會死)
+---------+ +---------+
| | | |
| | | |
| User |1 1| Book |
| |----- -----| |
+---------+ | | +---------+
| 1 | | | 1
| | | |
| | | |
| 1,* | | | 1,*
+---------+ | | 1,*+---------+
| | ---+----| |
| |1,* | | |
| Comment |--------- | Tag |
| | | |
+---------+ +---------+
我正在尋找如何在Rails的配置我的模型的任何建議。目前,我期待設置:
class Users
has_many :tags
has_many :comments
has_many :tagsBooks, :through=>tags, :class_name => "tags_books"
has_many :commentsBooks, :through=>comments, :class_name => "comments_books"
end
class Books
has_many :tags
has_many :comments
has_many :tagsUsers, :through=>tags, :class_name => "tags_users"
has_many :commentsUsers, :through=>comments, :class_name => "comments_users"
end
class Tags
belongs_top :users
belongs_to :books
end
class Comments
belongs_top :users
belongs_to :books
end
我相信「這將有助於在執行的標籤和註釋表的外鍵字段(USER_ID和Books_id) - 這是最終什麼,我需要做的。我希望這將允許我將標籤和評論與用戶和書籍(即books.tags或users.tags)相關聯。
這是正確的嗎?
我很高興,只要外鍵的分貝和Rails執行,以徹底改變設計。
乾杯,
達摩
是不是您可以重複使用它們的標籤的想法?或者,您是否在此上下文中定義了不同的標籤?我會創建標籤和書籍之間的多對多關係。 – xinit 2011-02-16 07:28:43