Does Agile Web Development With Ruby on Rails (Third Edition) teach best practices as well as Rails coding?
My concern is that, as I use this book, I'm developing bad Rails coding habits resulting from the rather basic nature of the examples used in the book. Case-in-point:
The Product Model:
class Product < ActiveRecord::Base
def self.find_products_for_sale
find(:all, :order => "title" )
end
The Store Controller
class StoreController < ApplicationController
def index
@products = Product.find_products_for_sale
end
end
The Store Index View
<h1>Your Pragmatic Catalog</h1>
<% @products.each do |product| -%>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%=h product.title %></h3>
<%= product.description %>
<div class="price-line">
<span class="price"><%= product.price %></span>
</div>
</div>
<% end %>
Is it a best practice to declare a 'helper' function of sorts just to pull in all the available products in the catalog? Shouldn't they have just done this?
@products = Products.find(:all, :order => "title")
;
I understand that they were probably just trying to demonstrate class-level methods, but they don't add any caveat in the code stating that this isn't really how you're supposed to do this.