[英]MongoDb complex query: How to query some “Page”s out of “Book”s belonging to a “User”
[英]Rails complex SQL for objects belonging to a user's followers
我試圖制定一個復雜的SQL查詢(在Rails 3中)以訪問屬於用戶關注者的照片對象。
該方案是:
因此,這基本上就像將所有關注者的照片收集到一個集合中,根據照片創建日期對集合進行排序,然后對結果進行分頁以一次僅返回10張照片。 這可能意味着最近的10張照片來自多個關注者。
我對此有兩個問題:
User.rb
文件中的關系建模為:
has_many :following, :through => :relationships, :source => :followed
has_many :followers, :through => :reverse_relationships, :source => :follower
has_many :photos, :dependent => :destroy, :order => 'created_at DESC'
關注者/關注者只是另一個用戶。
感謝您提供的任何指導! 非常感謝!!! :)
我懷疑會有延遲問題,因為在您的情況下,will_paginate對查詢設置了限制。但是,您需要確保數據庫中存在索引。
我認為這樣的事情應該起作用:
has_many :follower_photos, :class_name => 'Photo', :through => :followers
那是您要找的東西嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.