[英]How to order by the date of the last comment and sort by last created otherwise?
我有一個Post模型:
class Post < ActiveRecord::Base
attr_accessible :title, :content, :tag_names
belongs_to :user
has_many :comments, :dependent => :destroy
end
belongs_to :post, :counter_cache => true
belongs_to :user
end
和評論模型:
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :post, :counter_cache => true
belongs_to :user
end
現在,我知道如何按日期排序: created_at ASC
, created_at DESC
。
現在,我想知道,如何按最后評論的日期排序,並按最后創建的排序?
StackOverflow的功能差不多。 例如,如果一個問題得到答復,則該問題位於頂部,如果其他問題得到答復,則該問題位於頂部,依此類推。 否則,問題按ASC排序。
生成的SQL:
Started POST "/posts/30/comments" for 127.0.0.1 at 2012-03-12 11:59:32 +0800
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0zGpin7T/1aH/JBomGiqsEYCIgQ04HQWHZ/bIjm7WKs=", "comment"=>{"content"=>"sfsfwfewsdesd"}, "commit"=>"Create Comment", "post_id"=>"30"}
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "30"]]
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
(0.2ms) begin transaction
SQL (1.1ms) INSERT INTO "comments" ("content", "created_at", "post_id", "total_votes", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["content", "sfsfwfewsdesd"], ["created_at", Mon, 12 Mar 2012 03:59:32 UTC +00:00], ["post_id", 30], ["total_votes", 0], ["updated_at", Mon, 12 Mar 2012 03:59:32 UTC +00:00], ["user_id", 2]]
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 30 LIMIT 1
SQL (0.5ms) UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1 WHERE "posts"."id" = 30
(130.3ms) commit transaction
Redirected to http://localhost:3000/posts/30
Completed 302 Found in 156ms (ActiveRecord: 133.4ms)
如果您是我,我會在您的Post模型中添加一個last_comment_at:datetime字段,並確保在創建注釋時更新該datetime。
這樣,您可以簡單地使用Post.order('last_comment_at, created_at DESC')
更新-在回答您有關所需遷移的評論時,以下內容將為您生成遷移:
rails generate migration add_last_comment_at_to_posts last_comment_at:datetime
正如其他人指出的那樣,您可以在Comment模型中添加一個after_save調用,以更新Post模型中的last_comment_at字段。 我會用這樣的東西:
class Comment < ActiveRecord::Base
belongs_to :post
after_save :update_post_last_comment_at
private
def update_post_last_comment_at
self.post.touch(:last_comment_at) if self.post
end
end
首先,您需要在帖子中添加另一個datetime列。 我們稱它為content_changed_at
或類似名稱。 在新帖子中,其值為created_at
。 然后,每當有評論發布時,您就對其進行更新。 當您需要顯示帖子時,只需按此列對其進行排序。
這是未經測試的代碼,但是它可以帶給您想法。
class Post < ActiveRecord::Base
attr_accessible :title, :content, :tag_names
before_create :init_sort_column
belongs_to :user
has_many :comments, :dependent => :destroy
private
def init_sort_column
self.content_changed_at = self.created_at || Time.now
end
end
belongs_to :post, :counter_cache => true
belongs_to :user
end
class Comment < ActiveRecord::Base
attr_accessible :content, :user_id
after_create :update_parent_sort_column
belongs_to :post, :counter_cache => true
belongs_to :user
private
def update_parent_sort_column
if post
post.content_changed_at = self.created_at
end
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.