John Griffiths
2008-Feb-02 11:57 UTC
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 ? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
John Griffiths
2008-Feb-02 12:26 UTC
Re: sort notes by category (when category is stored in notes
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, -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ilan Berci
2008-Feb-03 00:35 UTC
Re: sort notes by category (when category is stored in notes
John Griffiths 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
John Griffiths
2008-Feb-04 12:02 UTC
Re: sort notes by category (when category is stored in notes
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() ? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Seemingly Similar Threads
- How to sort using a predefined criterion
- sip.conf and SIP client host= not recognized in some cases
- Can a matrix have 'list' as rows/columns?
- xyplot : superimposed 2 groups in different panels
- [WISH / PATCH] possibility to split string literals across multiple lines