# Sort notes by category (when category is stored in notes)

**URL:** <https://www.ruby-forum.com/t/sort-notes-by-category-when-category-is-stored-in-notes/127903>\
**Category:** Rails\
**Created:** [February 2, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/sort-notes-by-category-when-category-is-stored-in-notes/127903 "2008-02-02T12:57:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 2, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/sort-notes-by-category-when-category-is-stored-in-notes/127903/1 "2008-02-02T12:57:38Z")

</div>

okay, breaks the relationship model but it’s a really simple data model  
so doesn’t need too much about it.

here’s my structure, I have a notes model containing…

note:  
name, date, category

so each note will have it’s own, name, date and category (with some of  
the categories being the same, hence sort by…)

now i could sort by category easier by having the category stored within  
it’s own model, but for this experiment i’d like it within the same  
table as the notes.

how would i sort them fast and get all the notes related to categories  
which are the same…

cat: aaa

name: test, cat:aaa  
name: ddd, cat:aaa

cat: bbb

name: more stuff, cat:bbb  
name: testing, cat:bbb

?

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 2, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/sort-notes-by-category-when-category-is-stored-in-notes/127903/2 "2008-02-02T13:26:07Z")

</div>

okay, think i worked it out, the logic could make anyone’s eyes bleed  
but here’s how it goes…

def notes  
@notes = Notes.find(:all)  
end

then in the view…

\<% @notes.group\_by(&:category).each do |category, note| %\>

\<% for @not in note %\>

\<%= @not.name %\>  
\<%= @not.category %\>

\<% end %\>

\<% end %\>

now your notes are ordered by category, even though the cateogory is  
defined WITHIN the note model.

mental,

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 3, 2008, 1:35am UTC](https://www.ruby-forum.com/t/sort-notes-by-category-when-category-is-stored-in-notes/127903/3 "2008-02-03T01:35:01Z")

</div>

John G. wrote:

> okay, breaks the relationship model but it’s a really simple data model  
> so doesn’t need too much about it.
> 
> here’s my structure, I have a notes model containing…
> 
> note:  
> name, date, category
> 
> so each note will have it’s own, name, date and category (with some of  
> the categories being the same, hence sort by…)
> 
> now i could sort by category easier by having the category stored within  
> it’s own model, but for this experiment i’d like it within the same  
> table as the notes.
> 
> how would i sort them fast and get all the notes related to categories  
> which are the same…
> 
> cat: aaa
> 
> name: test, cat:aaa  
> name: ddd, cat:aaa
> 
> cat: bbb
> 
> name: more stuff, cat:bbb  
> name: testing, cat:bbb
> 
> ?

look at the rails api ActiveRecord::Base.find() method… it take a  
:group and :sort option which is what I think you are looking for here…

hth…

ilan

---

<div class="post-metadata">

**Author:** ![softf5ajivanet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9f8e36/32.png) [@softf5ajivanet](https://www.ruby-forum.com/u/softf5ajivanet)\
**Post date:** [February 4, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/sort-notes-by-category-when-category-is-stored-in-notes/127903/4 "2008-02-04T13:02:08Z")

</div>

will do Ilan, I’ve got the problem sorted at the moment but imagine i’d  
need to look at this in more depth later.

I’ll have a look at :group and :sort, thanks for that.

Any links for Base.find() ?
