Is there a pattern to refactor such a construct into a readable single line?
def show_section
@news = News.all_active
@news = @news.where(:section => params[:section]) unless params[:section] == "all"
@news = @news.all
end
I use Rails 3 and Ruby 1.9.2
You can get rid of
@news.all– in Rails 3 query will be executed when you use the resultingActiveRecord::Relationobject (for example when you calleachorfirston it). Passingniltowheremethod will do nothing.If
all_activeis a method, you can refactor it into a scope and then call it in a chain.Great resources on Rails 3 queries: