Forum: Ruby on Rails Code in layout files, and minimizing database usage.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-05-28 01:33
So, I'm putting together a blog, and I want the posts on it to be easily
browsable by date. So far I've given each post attributes for the month
and year it was created. That way, when someone visits
example.com/blog/2008 Rails does a find_all_by_year and displays and
paginates the results - similarly, when someone visits
example.com/blog/2008/09 Rails does find_all_by_year_and_month and does
the same.

Now, I'm trying to change the blog layout so that it provides links to
each month as well as the post count for that month. This would look
something like:

August 2008 (24)
September 2008 (33)
October 2008 (17)

And so on. So, two questions:

#1 - What's the best way to do this? The most direct method I see is to
do a Post.find_all_by_year_and_month(...).count for each month of each
year, but that would be pretty abusive of the database, right?

#2 - How do I do this and remain MVC? The above display is happening in
the layout, and I know I shouldn't be getting very code-heavy in the
view, but I don't know where else to put this kind of functionality.

Thanks!
Chris
710eb9516d466362bc80a2f8d3c31cab?d=identicon&s=25 Chris Hanks (preciousbodilyfluids)
on 2009-05-28 03:06
Ok, after some thinking and experimentation, I came up with:

<% posts = Post.all(:select => "year, month", :order => "year,
month").collect(&:attributes) %>
<% (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.
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-05-28 05:57
Chris Hanks wrote:
[...]
> It's working well. It's all still stuck in the view, but oh well.

That's way too much logic to put in a view, even by my standards. :)

> If
> anyone knows a better place, let me know.

The easiest refactoring would be to put it in a helper.  Or perhaps you
can get creative with a class method on Post.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-05-28 09:18
(Received via mailing list)
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
29ebf90af6107d2eb39b587c7972639c?d=identicon&s=25 Mukund (Guest)
on 2009-05-28 10:04
(Received via mailing list)
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"

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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-28 10:24
(Received via mailing list)
On May 28, 9:03 am, Mukund <marut...@yahoo.com> 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
This topic is locked and can not be replied to.