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

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

?

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,

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

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() ?