Skip to content

Latest commit

 

History

History
40 lines (27 loc) · 1.36 KB

denormalization_and_recent_comments.md

File metadata and controls

40 lines (27 loc) · 1.36 KB

←   documentation

Denormalization

For building of Recent comments list (for polymorphic relationship) we need to have many additional requests to database. It's classic problem of polymorphic comments.

I use denormalization of commentable objects to solve this problem.

My practice shows - We need 3 denormalized fields into comment for (request-free) building of recent comments list:

the_comments

  • Comment#commentable_title - for example: "My first post about Ruby On Rails"
  • Comment#commentable_url - for example: "/posts/1-my-first-post-about-ruby-on-rails"
  • Comment#commentable_state - for example: "draft"

That is why any Commentable Model should have few methods to provide denormalization for Comments.

Recent comments building

Denormalization makes building of Recent comments (for polymorphic relationship) very easy!

Controller:

@comments = Comment.with_state(:published)
                   .where(commentable_state: [:published])
                   .order('created_at DESC')
                   .page(params[:page])

View:

- @comments.each do |comment|
  %div
    %p= comment.commentable_title
    %p= link_to comment.commentable_title, comment.commentable_url
    %p= comment.content