मैं सभी current_user.friends स्थितियों लाने और फिर created_at द्वारा उन्हें सुलझाने के लिए की जरूरत है।रेल: स्थिति के साथ स्थिति की तुलना में विफल रहा है
class User < ActiveRecord::Base
has_many :statuses
end
class Status < ActiveRecord::Base
belongs_to :user
end
और नियंत्रक में:
def index
@statuses = []
current_user.friends.map{ |friend| friend.statuses.each { |status| @statuses << status } }
current_user.statuses.each { |status| @statuses << status }
@statuses.sort! { |a,b| b.created_at <=> a.created_at }
end
current_user.friends
ऑब्जेक्ट की श्रृंखला User
friend.statuses
ऑब्जेक्ट की श्रृंखला रिटर्न देता है Status
त्रुटि:
comparison of Status with Status failed
app/controllers/welcome_controller.rb:10:in `sort!'
app/controllers/welcome_controller.rb:10:in `index'
आपकी मूल समस्या नहीं है लेकिन उन सभी संगठनों को चलने से आपको प्रश्नों की संख्या बहुत अधिक होगी। सभी दोस्तों और उनकी सभी स्थितियों को पकड़कर और फिर उन्हें कोड में सॉर्ट करने से असंगत संख्या में प्रश्न मिल सकते हैं, आपका प्रदर्शन तेजी से गिरने वाला है। क्यों न सिर्फ एक ही SQL क्वेरी लिखें जो उन सभी रिकॉर्ड्स को एक साथ ला सकता है और सॉर्ट कर सकता है? –
धन्यवाद, मुझे एक आसान तरीका और प्रभावी तरीका मिला: 'Status.where (user_id: current_user.friends.map (&: id) .insert (0, current_user.id))। सभी 'आपको क्या लगता है? – Alex