Profile können Beiträge erstellen. Profile können Beiträge kommentieren. Gewichtung: Post = 1, Kommentar = 1.Rails - Auflistung der meisten aktiven Profil basierend auf der Anzahl der Beiträge, Kommentare
Ich muss Personen basierend auf konsolidierte Gewichtung der Beiträge, Kommentare innerhalb letzten Monats auflisten.
, was ich versucht habe, ist:
profiles = Profile.find(:all, :include => [:posts, :comments], :conditions => ["posts.created_at > ? or comments.created_at > ?", 1.month.ago, 1.month.ago], :group => "profiles.id", :group => 'profiles.id, posts.id, comments.id', :limit => 5)
und
sql = "SELECT DISTINCT profiles.id, count(posts.id) + count(comments.id) as pcount FROM profiles LEFT OUTER JOIN posts ON posts.profile_id = profiles.id LEFT OUTER JOIN comments ON comments.profile_id = profiles.id WHERE (posts.created_at > '#{1.month.ago}' and posts or comments.created_at > '#{1.month.ago}') GROUP BY profiles.id Order by pcount"
profiles = ActiveRecord::Base.connection.execute(sql)
Sie können zwei Attribute in Ihrer Profiltabelle haben:: comments_count &: posts_count, Und jedes Mal, wenn ein Benutzer einen Beitrag oder Kommentar schreibt, können Sie Rails-Callbacks verwenden, um diese Werte zu erhöhen. –
In diesem Fall konnten wir den Zeitpunkt des Posts oder Kommentars nicht lesen. Ich brauche die Liste der Top-Beitragenden des letzten Monats. –