Im new in rails and ruby! I have a database with records about articles. Each article has the following fields: - tittle - author - body
I would like to render in the index
page a list of the count of ALL articles with the same "tittle" and the same "author". Example:
Database:
tittle: "dogs", author: "john", body: "dogs are nice",
tittle: "cats", author: "john", body: "cats are nice",
tittle: "dogs", author: "Lisa", body: "dogs are bad",
tittle: "dogs", author: "john", body: "dogs are nice",
tittle: "dogs", author: "john", body: "dogs are nice",
tittle: "dogs", author: "Lisa", body: "dogs are bad"
Now in my index page I would like to list in the occurrence desc order like this:
Articles:
tittle Author how many times
- "dogs" "john" 3
- "dogs" "Lisa" 2
- "cats" "Lisa" 1
I was thinking about enumerable: 'group_by'
, or a query Article.select([:id, :name])
? however, I don't know how to apply it to my code.
Currently my controller looks like this:
def index
@articles = Article.order(created_at: :desc)
end
I would really appreciate ideas or solutions on how to solve this as painless as possible and keep learning from this.