2012-08-25 215 views
1

我已經建立了一個類似於facebook的線程消息系統,其中一個用戶有許多對話正在同時進行。每個對話都有一組獨特的人蔘與,每個都包含多個消息。有很多:通過,更好的方法來做到這一點?

class User < ActiveRecord::Base 
    has_many :involvements 
    has_many :conversations, through: :involvements, unique: true 
end 

class Involvement < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :involvable, polymorphic: true 
end 

class Conversation < ActiveRecord::Base 
    has_many :involvements, as: :involvable 
    has_many :participants, through: :involved, class_name: "User", unique: true 
    has_many :messages 
end 

class Message < ActiveRecord::Base 
    belongs_to :conversation 
end 

我得到了它這方面的工作,但我不禁感到像,必須有一些更好的,更有效的方法壓縮此設置,例如使用3個模型。或者可能使用獨特的列或其他東西habtm ...

發送消息的四個模型似乎太多了。有什麼建議麼?或者這是我不應該擔心的事情?

+2

爲什麼4看起來太多了?這裏有4條數據:用戶,他們參與對話,對話以及這些對話中的消息。你的設置看起來非常合理。 – deefour

回答

相關問題