0
嗨我目前正在用rails(3)構建一個小的論壇應用程序。我在Rails的問題上相當新,當我想拉動由論壇ID指定的頂層板(一個板屬於論壇)時,我陷入了困境。計數軌道中的對象的孫子女
「定義頂板」:屬於具有最多topic_replies &主題的特定forum_id的板子。
「家族樹」:論壇>主板>主題> TopicReply
我的模型:
**forum.rb**
class Forum < ActiveRecord::Base
default_scope :order => 'display_order ASC'
has_many :boards, :dependent => :destroy
end
**board.rb**
class Board < ActiveRecord::Base
default_scope :order => 'display_order ASC'
belongs_to :forum
has_many :topics, :dependent => :destroy
has_many :topic_replies, :through => :topics
def latest_topic_reply
t = TopicReply.find_by_sql("SELECT tr.* from topic_replies tr, topics t where tr.topic_id = t.id AND t.board_id = #{self.id} ORDER BY tr.updated_at desc LIMIT 1;")[0]
end
end
**topic.rb**
class Topic < ActiveRecord::Base
belongs_to :board
has_many :topic_replies, :dependent => :destroy
end
**topic_reply.rb**
class TopicReply < ActiveRecord::Base
belongs_to :topic
end
在SQL我這樣做:
"SELECT b.* FROM boards b,topics t WHERE t.board_id=b.id AND b.forum_id=2 GROUP BY board_id ORDER BY SUM(t.topic_replies_count) DESC LIMIT 4;"
我更願意管理這種儘管有活動記錄(或者這不比SQL更好),但我還不太熟悉它。有誰能把我推向正確的方向嗎?
在此先感謝
感謝您對緩存列的參考,目前還不知道。仍然沒有找到正確的ActiveRecord,但我添加了正確的SQL。 – Ayrton 2010-11-25 15:43:37