我有5個模型:用戶,關鍵字,問題,關聯和question_status。左或全加入活動記錄
- 用戶和關鍵字由協會(同樣的關鍵字可以由多個用戶 添加&反之亦然)
- 關鍵字和問題通過的has_many belongs_to的(關鍵字 可以有很多問題,問題涉及有關屬於關鍵字)
- 用戶和問題由question_status(用戶可以標記 問題的回答,刪除等)
所以RELA相關tionships是:
- 用戶有通過協會的關鍵字
- 關鍵字有通過協會衆多用戶
- 關鍵字,有許多問題
- 問題屬於一個關鍵字
- 的用戶可以將狀態許多問題通過question_status
- 一個問題可以由許多用戶通過分配status_status
我不得不建造5個模型,因爲同一個關鍵字可以被多個用戶添加。所以我必須保持用戶和關鍵字彼此分離,並通過關聯使它們相關。同樣,多個用戶可以在同一個問題上擁有不同的標籤(回答,刪除,存檔等)。所以我必須將這些狀態與問題表分開保存。
模型是 -
class User < ActiveRecord::Base
has_many :associations, dependent: :destroy
has_many :keywords, :through => :associations
has_many :question_statuses, dependent: :destroy
has_many :questions, :through => :question_statuses
class Keyword < ActiveRecord::Base
has_many :questions, dependent: :destroy
has_many :associations, dependent: :destroy
has_many :users, :through => :associations
class Question < ActiveRecord::Base
belongs_to :keyword
has_many :question_statuses, dependent: :destroy
has_many :users, :through => :question_statuses
class Association < ActiveRecord::Base
belongs_to :keyword
belongs_to :user
class QuestionStatus < ActiveRecord::Base
belongs_to :question
belongs_to :user
我需要建立在ActiveRecord的連接查詢,這將使我的問題的用戶(例如users.id = 2)關鍵字用戶在自己的帳戶添加排除已刪除或回答的問題,例如已刪除= t或回答= t在question_statuses表中。
只有當用戶標記問題時,記錄纔會添加到question_statuses中。否則,question_statuses將沒有該用戶的任何記錄。
任何幫助將不勝感激。
你提到了刪除= t和回答= t。那些在Rails中的布爾字段?如果您使用顯式文本字段而不是布爾字段,我建議切換到使用布爾字段,以便Rails能夠理解您打算在其中存儲什麼類型的信息。 – Coenwulf
他們是布爾字段。 – amey1908