Re: Question on the "common way" to code

s.ross
Sun, 22 Jul 2007 06:44:32 -0000

(1) It depends. The first allows the database to do the sorting,
which is almost always a good thing. The second performs the sorting
in memory, and is thus likely to be slower. I''ve seldom seen the
second method employed in real code because databases are so good at
sorting.

(2) In RHTML, many people use <% for composer in @composers -%> I
don''t. I don''t use for in Ruby. So my looping code is usually
<%
@composers.each do |composer| -%>. That said, it is often cleaner to
render collections in partials. That would look like this: <%=
render :partiai => ''composer'', :collection => @composers
%>.

(3) Whatever works. Either works fine, both are ugly but necessary.
Better, in your model do this:

def full_name
   first_name + last_name
end

Then in your view, you can just write:

<%= link_to composer.full_name, composers_url(composer) %>

Just some thoughts.

On Jul 21, 2007, at 8:34 PM, nemesis256 wrote:

>
> So I posted last week asking about new learning resources.  I watched
> the lynda.com RoR videos, and now I just started reading the Ruby for
> Rails book.  Both of these sources code in different ways, and I was
> wondering which way is most common.  In other words, is one of these
> ways frowned upon in the Rails community?  Keep in mind that these are
> rather specific examples and not necessarily complete lines of code.
>
> 1. First is how things are read from a database, or more specifically,
> sorting.  One does this
> @albums = Album.find(:all, :order => "title ASC")
> while the other does this
> @composers = Composer.find(:all).sort_by{|c| [c.last_name,
> c.first_name]}
>
> 2. Looping.
> <% for composer in @composers -%>
> and
> @composers.each do |composer|
>
> 3. dynamic links
> link_to(composer.first_name + " " + composer.last_name
> and
> link_to("#{composer.first_name} #{composer.last_name}"
>
> In all cases I prefer the first (lynda.com) method, either because
> it''s shorter or because it requires less symbols.  The Ruby for
Rails
> method seems to be a lot more object focused.  So which of these do
> you see more often or is the common way?  Or does it not matter?
>
>
> >