At the risk of asking dumb questions (i.e., those answered in a FAQ somewhere), I submit the following for yourreflection, consideration, or ridicule, as the whim or preponderance of knowlege strikes you :) I''m writing yet another blogging tool, more as a learning experience than anything else - and its going swimmingly so far, the project is really coming along in many ways. So well in fact, that functional matters are beginning to be outnumbered by cosmetic ones. My problem is that when the blog is displayed in the browser, the entries are not displayed last to first, but rather first to last. There are arguments in favor of doing it both ways. I for one think it should be a preferences feature. In any case, I still need to know how to accomplish it. The code currently displaying the blog entries is really trivial: <table width="755px" cellpadding="3px"> <% @posts.each do |post| %> <tr> <td width="200px"><div id=poster><%= post.poster %> -<br><%= post.category.categorytext %></div></td> <td><div id=postheader><%= link_to post.heading, :action => "show", :id => post.id %></div></td> </tr> <tr> <td valign="top"><div id=datestyle><%= post.dateposted %></div></td> <td width="835px" colspan=2><div id=postcontent><%= textilize_without_paragraph(post.posttext) %></div><br></td> </tr> <tr> <% end %> </table> What I''d like to do is keep it very simple; in my minds eye I see something like: <table width="755px" cellpadding="3px"> <% @posts.each do |post| :reverse %> <!-- ''''hypothetical'''' reverse keyword added to each: posts loop --> <tr> <td width="200px"><div id=poster><%= post.poster %> -<br><%= post.category.categorytext %></div></td> <td><div id=postheader><%= link_to post.heading, :action => "show", :id => post.id %></div></td> </tr> <tr> <td valign="top"><div id=datestyle><%= post.dateposted %></div></td> <td width="835px" colspan=2><div id=postcontent><%= textilize_without_paragraph(post.posttext) %></div><br></td> </tr> <tr> <% end %> </table> Then I could extend it to the configuration point by adding some conditional execution to it based on the preference setting. That would be too easy though. Anyone interested in cluing me in on what is essentially a Ruby programming question? Thanks, you guys Rock (and you Grrlz too :) Be Well, Twitch
James G. Stallings II wrote:> At the risk of asking dumb questions (i.e., those answered in a FAQ > somewhere), I submit the following for yourreflection, consideration, or > ridicule, as the whim or preponderance of knowlege strikes you :) > > I''m writing yet another blogging tool, more as a learning experience > than anything else - and its going swimmingly so far, the project is > really coming along in many ways. So well in fact, that functional > matters are beginning to be outnumbered by cosmetic ones. > > My problem is that when the blog is displayed in the browser, the > entries are not displayed last to first, but rather first to last. There > are arguments in favor of doing it both ways. I for one think it should > be a preferences feature. In any case, I still need to know how to > accomplish it. > > The code currently displaying the blog entries is really trivial: > > <table width="755px" cellpadding="3px"> > <% @posts.each do |post| %> > <tr> > <td width="200px"><div id=poster><%= post.poster %> -<br><%= > post.category.categorytext %></div></td> > <td><div id=postheader><%= link_to post.heading, :action => "show", > :id => post.id %></div></td> > </tr> > <tr> > <td valign="top"><div id=datestyle><%= post.dateposted %></div></td> > <td width="835px" colspan=2><div id=postcontent><%= > textilize_without_paragraph(post.posttext) %></div><br></td> > </tr> > <tr> > <% end %> > </table> > > What I''d like to do is keep it very simple; in my minds eye I see > something like: > > <table width="755px" cellpadding="3px"> > <% @posts.each do |post| :reverse %> <!-- > ''''hypothetical'''' reverse keyword added to each: posts loop --> > <tr> > <td width="200px"><div id=poster><%= post.poster %> -<br><%= > post.category.categorytext %></div></td> > <td><div id=postheader><%= link_to post.heading, :action => "show", > :id => post.id %></div></td> > </tr> > <tr> > <td valign="top"><div id=datestyle><%= post.dateposted %></div></td> > <td width="835px" colspan=2><div id=postcontent><%= > textilize_without_paragraph(post.posttext) %></div><br></td> > </tr> > <tr> > <% end %> > </table> > > Then I could extend it to the configuration point by adding some > conditional execution to it based on the preference setting. That would > be too easy though. > > Anyone interested in cluing me in on what is essentially a Ruby > programming question? > > Thanks, you guys Rock (and you Grrlz too :) > > Be Well, > TwitchThe easiest way would be to order it when you make the query. eg @posts = Post.find_all(nil, ''created_at DESC'') You could set DESC or ASC depending on some configuration or preference. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Scott Barron wrote:> James G. Stallings II wrote: > >> At the risk of asking dumb questions (i.e., those answered in a FAQ >> somewhere), I submit the following for yourreflection, consideration, >> or ridicule, as the whim or preponderance of knowlege strikes you :) >> >> I''m writing yet another blogging tool, more as a learning experience >> than anything else - and its going swimmingly so far, the project is >> really coming along in many ways. So well in fact, that functional >> matters are beginning to be outnumbered by cosmetic ones. >> >> My problem is that when the blog is displayed in the browser, the >> entries are not displayed last to first, but rather first to last. >> There are arguments in favor of doing it both ways. I for one think >> it should be a preferences feature. In any case, I still need to know >> how to accomplish it. >> >> The code currently displaying the blog entries is really trivial: >> >> <table width="755px" cellpadding="3px"> >> <% @posts.each do |post| %> >> <tr> >> <td width="200px"><div id=poster><%= post.poster %> -<br><%= >> post.category.categorytext %></div></td> >> <td><div id=postheader><%= link_to post.heading, :action => >> "show", :id => post.id %></div></td> >> </tr> >> <tr> >> <td valign="top"><div id=datestyle><%= post.dateposted %></div></td> >> <td width="835px" colspan=2><div id=postcontent><%= >> textilize_without_paragraph(post.posttext) %></div><br></td> >> </tr> >> <tr> >> <% end %> >> </table> >> >> What I''d like to do is keep it very simple; in my minds eye I see >> something like: >> >> <table width="755px" cellpadding="3px"> >> <% @posts.each do |post| :reverse %> <!-- >> ''''hypothetical'''' reverse keyword added to each: posts loop --> >> <tr> >> <td width="200px"><div id=poster><%= post.poster %> -<br><%= >> post.category.categorytext %></div></td> >> <td><div id=postheader><%= link_to post.heading, :action => >> "show", :id => post.id %></div></td> >> </tr> >> <tr> >> <td valign="top"><div id=datestyle><%= post.dateposted %></div></td> >> <td width="835px" colspan=2><div id=postcontent><%= >> textilize_without_paragraph(post.posttext) %></div><br></td> >> </tr> >> <tr> >> <% end %> >> </table> >> >> Then I could extend it to the configuration point by adding some >> conditional execution to it based on the preference setting. That >> would be too easy though. >> >> Anyone interested in cluing me in on what is essentially a Ruby >> programming question? >> >> Thanks, you guys Rock (and you Grrlz too :) >> >> Be Well, >> Twitch > > > The easiest way would be to order it when you make the query. eg > > @posts = Post.find_all(nil, ''created_at DESC'') > > You could set DESC or ASC depending on some configuration or preference. > > -Scott > >------------------------------------------------------------------------ > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >Ah yes. That was the approach I attempted to take initially; after all it reflects the abstraction philosophy of offloading/delegating tasks to appropriate cooperative applications; in this case, we''d ideally let the SQL database do the database-oriented task of ordering the data for presentation. BUT, in the case of MySQL, there is no way to change the collation order of an index from ascending to descending! Ridiculous, you might say, and I''d agree; but here it is, straight from the mouth of the horse in question: "An index_col_name specification can end with ASC or DESC. These keywords are allowed for future extensions for specifying ascending or descending index value storage. Currently they are parsed but ignored; index values are always stored in ascending order." -MySQL Manual, http://dev.mysql.com/doc/mysql/en/create-table.html Those keywords didn''t even parse for me, but what the hell - I''d rather get an error message than have them silently ignored and pull my hair out for weeks trying to figure out why my posts kept coming out in forward order :P It may be that other options exist for getting this done with SQL, such as some sort of ORDER BY clause which I just noticed on the same page of documentation, or by using a different table/index type than whatever defaults were created when I began the project. Thanks for your comments : ) Twitch
> My problem is that when the blog is displayed in the browser, the > entries are not displayed last to first, but rather first to last. There > are arguments in favor of doing it both ways. I for one think it should > be a preferences feature. In any case, I still need to know how to > accomplish it.> <% @posts.each do |post| %>You can reverse an array with the reverse-method before you pass it to the each-method: <% @posts.reverse.each do |post| %> Michael
Michael Raidel wrote:>>My problem is that when the blog is displayed in the browser, the >>entries are not displayed last to first, but rather first to last. There >>are arguments in favor of doing it both ways. I for one think it should >>be a preferences feature. In any case, I still need to know how to >>accomplish it. >> >> > > > >><% @posts.each do |post| %> >> >> > >You can reverse an array with the reverse-method before you pass it to the >each-method: > ><% @posts.reverse.each do |post| %> > >Michael > >See I knew it was bound to be that easy; worked like a freakin'' charm. Thanks Michael! Twitch
James G. Stallings II wrote:> > BUT, in the case of MySQL, there is no way to change the collation order > of an index from ascending to descending! Ridiculous, you might say, and > I''d agree; but here it is, straight from the mouth of the horse in > question: > "An index_col_name specification can end with ASC or DESC. These > keywords are allowed for future extensions for specifying ascending > or descending index value storage. Currently they are parsed but > ignored; index values are always stored in ascending order." > > -MySQL Manual, http://dev.mysql.com/doc/mysql/en/create-table.html > > Those keywords didn''t even parse for me, but what the hell - I''d rather > get an error message than have them silently ignored and pull my hair > out for weeks trying to figure out why my posts kept coming out in > forward order :P > > It may be that other options exist for getting this done with SQL, such > as some sort of ORDER BY clause which I just noticed on the same page of > documentation, or by using a different table/index type than whatever > defaults were created when I began the project.Er... the ORDER BY clause is how it''d be done, and that''s what the second argument to find_all is. The created_at timestamp would not be an index, the area of the manual you''ve quoted has to do with storage of indexes, not data retrieval ordering. Post.find_all(nil, ''created_at DESC'') tranlsates into: SELECT * FROM posts ORDER BY created_at DESC And that''s how you''d want to order your data. I don''t believe that SQL will guarantee any kind of ordering if you do not specify an ORDER BY clause. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Michael Raidel wrote:> You can reverse an array with the reverse-method before you pass it to the > each-method: > > <% @posts.reverse.each do |post| %>Minor suggestion: Use the .reverse_each form which does the same without constructing an intermediate Array.
.reverse_each I knew there was a reason, I like to read all of these posts! -Dale On Sun, 30 Jan 2005 19:51:52 +0100, Florian Groß <florgro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Michael Raidel wrote: > > > You can reverse an array with the reverse-method before you pass it to the > > each-method: > > > > <% @posts.reverse.each do |post| %> > > Minor suggestion: Use the .reverse_each form which does the same without > constructing an intermediate Array. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- This is my sig. Isn''t it cool....