On May 28, 9:03 am, Mukund <[email protected]> wrote:
> Why do you want to load all the posts into memory? For doing a
> count, rely on SQL instead of Ruby. It is faster and more efficient.
>
> Post.count(:conditions=>"xxx") translates into a select count(*) from
> posts where "xxx"
>
And you could even do Post.count(:all, :group => 'year, month' ) (and
then cache that :-) )
Fred
> When someone clicks on the month title, you can always do a query with
> a :limit to show the last 10 posts and a "all" link to show all posts
> in that month.
>
> I would have class level functions in the Post model provide me
> information regarding count and data. The model alone knows how to
> get that data, other parts of the application have no need to dig into
> those details. This will ensure your app doesn't break if you change
> the implementation of Post in the future.
>
> On May 28, 12:17 pm, Julian Leviston <[email protected]> wrote:
>
> > On 28/05/2009, at 11:06 AM, Chris Hanks wrote:
>
> > > Ok, after some thinking and experimentation, I came up with:
>
> > > <% posts = Post.all(:select => "year, month", :order => "year,
> > > month").collect(&:attributes) %>
>
> > Any time you're doing equalities against a model object (here Post),
> > you could put it in the controller and assign it to an instance
> > varaible.
>
> > Also I'm not sure why you're getting the attributes of the objects,
> > when you could be getting the objects by themselves.
>
> > controller:
> > @posts = Post.all(:select => "year, month", :order => "year DESC,
> > month DESC")
>
> > view:
>
> > <% @posts.group_by(&:year).each do |year, year_of_posts | %>
> > <%= year %>
> > <% year_of_posts.group_by(&:month) do |month, month_of_posts| %>
> > <% month_of_posts.each do |post| %>
> > <%= "some data about the posts or links or whatever
> > %>
> > <% end %>
> > <% end %>
> > <% end %>
>
> > obviously you should change this to be more in line with what you
> > actually want (probably the inner each is unnecessary for your
> > requirements.
>
> > Rather than reversing in ruby, use order with DESC sql fragment to get
> > the data the way you want it from the database. This is what the
> > database is good at.
> > Also, have you thought of actually storing a datetime for the post,
> > instead of doing all this crazy number stuff? Then you could use the
> > strftime method on the datetime to print out the dates nicely. Much
> > more elegant.
>
> > Julian.
>
> > ----------------------------------------------
> > Learn:http://sensei.zenunit.com/
> > Last updated 20-May-09 (Rails, Basic Unix)
> > Blog:http://random8.zenunit.com/
> > Twitter:http://twitter.com/random8r
>
> > > <% (posts.first["year"]..posts.last["year"]).to_a.reverse.each do |
> > > year|
> > > %>
> > > <p><%= year %></p>
> > > <% posts_by_year = posts.select{|hash| hash["year"] == year} %>
> > > <ul class="nostyle">
> > > <% (1..12).to_a.reverse.each do |index| %>
> > > <% posts_by_month = posts_by_year.select{|hash| hash["month"] ==
> > > ("%02d" % index)}%>
> > > <% unless posts_by_month.length == 0 %>
> > > <li><%= link_to "#{Date::MONTHNAMES[index]} #{year}",
> > > month_path(year, ("%02d" % index)) %> (<%= posts_by_month.length
> > > %>)</li>
> > > <% end %>
> > > <% end %>
> > > </ul>
> > > <% end %>
>
> > > It's working well. It's all still stuck in the view, but oh well. If
> > > anyone knows a better place, let me know.
> > > --
> > > Posted viahttp://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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---