2
class Todo < ActiveRecord::Base
belongs_to :user
belongs_to :assignee, :class_name => "User"
has_many :comments, :dependent => :destroy
has_many :subscribers, :class_name => "User"
end
class User < ActiveRecord::Base
has_many :todos
has_many :assigned_todos, :class_name => 'Todo', :foreign_key => :assignee_id, :dependent => :destroy
has_many :comments, :through => :todos
has_many :subscriptions, :through => :todos, :inverse_of => :subscribers
end
我想讓用戶訂閱todos。簡單的導軌模型 - 訂閱用戶線程
我希望我可以做@ todo.subscribers,並得到用戶的列表回來。
問題:
- 是我的階級關係是否正確?
- 什麼數據庫結構,我需要爲用戶,如果有的話?
- 如果有更好的方法,請讓我知道。
你試圖通過運行這個控制檯,看看它是否按照你想要的方式行事? –
是的,我還沒有爲訂戶添加表,因爲我不確定該模式應該是什麼。 – cjm2671
他們訂閱了什麼?待辦事項?你應該把它看作是模型叫做TodosUsers。 –