I'd probably go with a slight variation of the first one:
def list
@pages = Page.order("pages.position ASC")
if (params.has_key?(:subject_id))
@subject = params[:subject_id]
@pages = @pages.where(:subject_id => params[:subject_id])
end
end
In either case, @pages
won't hit the database until you try to iterate over it so both versions will be the same as far as the database is concerned. Furthermore, these two should be identical (or as close to identical as to not matter) as far as Ruby is concerned:
x = M.order(...).where(...)
# and
x = M.order(...)
x = x.where(...)
so the above version of list
is DRYer and should perform exactly the same as the more repetitive version.