0
文件:
has_many :shares
has_many :groups, :through => :shares
用戶:
has_many :memberships
has_many :groups, :through => :memberships
組:
has_many :memberships
has_many :users, :through => :memberships
has_many :shares
has_many :documents, :through => :shares
我需要證明的唯一證件給出User,current_user的實例。
@documents = current_user.groups.documents
不會削減它。
我如何過濾只有至少與用戶共同組的文檔?
問題與做法是,如果一個文件有2組共同與用戶其輸出文檔的2倍。添加.uniq到最後修復它,但我不確定這是獲得結果的最佳方式。無論如何,感謝您的輸入! –
我同意用更簡潔的方式做它會很好,但性能方面的關鍵問題是它對數據庫的查詢。通過這種方法,您可以得到兩個,首先找到與'current_user'關聯的組(通過'memberships'),然後選擇具有這些組的所有文檔(通過'shares')。這是達到理想效果所需的最低限度。 –
更新了我的答案:您可以使用單個數據庫查詢來完成此操作。 –