Activerecord and empty find results

is this the prefered way to render a special rhtml-file if a
Record.find leads to an empty result?

def dummy
@users = User.find(:all, :conditions => “username = ‘dsr321’”)
if @users.blank?
redirect_to :action => “emptyresult”
end
end

Jochen K. wrote:

is this the prefered way to render a special rhtml-file if a
Record.find leads to an empty result?

def dummy
@users = User.find(:all, :conditions => “username = ‘dsr321’”)
if @users.blank?
redirect_to :action => “emptyresult”
end
end

I usually use the same template with some conditional logic to check if
the resultset is empty. You could do what you’re suggesting if you want
a totally different view. Or you could just choose to render another
template (IIRC this should do it, check the API otherwise):

render :template => “mycontroller/emptyresult”


Cheers,

  • Jacob A.

end
render :template => “mycontroller/emptyresult”
Ok, but is this the prefered syntax:

if @users.blank?

or should I use something different?

When I render the template with an empty result set I get “nil” but
“if @users.nil?” does not work.

On 7/23/07, Jacob A. [email protected] wrote:

  end

end

I usually use the same template with some conditional logic to check if
the resultset is empty. You could do what you’re suggesting if you want
a totally different view. Or you could just choose to render another
template (IIRC this should do it, check the API otherwise):

render :template => “mycontroller/emptyresult”

The advice from http://therailsway.com/ is to avoid conditional logic
in views with more than one branch. It’s OK to say “display x if y”
but it’s not OK to say “if x, display y, else display z”. Based on
that, I’d avoid having this decision in the template itself.

I’m not sure what the conventional wisdom is re: whether to redirect
to a different action or simply render a different template, but I
would (personally) favor rendering the different template unless there
was additional preparation necessary which might cloud the meaning of
the first action.

David

On 7/23/07, Jochen K. [email protected] wrote:

         redirect_to :action => "emptyresult"

render :template => “mycontroller/emptyresult”

Ok, but is this the prefered syntax:

if @users.blank?

or should I use something different?

When I render the template with an empty result set I get “nil” but
“if @users.nil?” does not work.

Assuming you’re doing something like @users = User.find(…), you
should have an empty array. If you’re doing something different please
post it. Otherwise I’d try @users.empty?

Also - you shouldn’t be getting nil from @users. Are you explicitly
referencing one of them in the view? Like this: @users[0]?

David C. wrote:

  end

but it’s not OK to say “if x, display y, else display z”. Based on
that, I’d avoid having this decision in the template itself.

Move it into a helper then.

I’m not sure what the conventional wisdom is re: whether to redirect
to a different action or simply render a different template, but I
would (personally) favor rendering the different template unless there
was additional preparation necessary which might cloud the meaning of
the first action.

I agree that render is better than redirect to avoid the extra http
request. But note that the idea of having different templates / actions
based on the number of results in a resultset breaks down as soon as the
number of resultsets displayed grows due to the combinatorial explosion.

That is if you wants to display both users and posts you will have to
create 4 templates, add another variable and this number grows to 8 -
not to mention the logic in the controller to handle this.


Cheers,

  • Jacob A.
 @users = User.find(:all, :conditions => "username =

want
or should I use something different?

When I render the template with an empty result set I get “nil”
but
“if @users.nil?” does not work.

Assuming you’re doing something like @users = User.find(…), you
should have an empty array.

I use this one:

unless @users.empty?
redirect_to :action => “list”
else
redirect_to :action => “fehler”
end

and everything works fine.

If you’re doing something different
please post it. Otherwise I’d try @users.empty?

Also - you shouldn’t be getting nil from @users. Are you
explicitly referencing one of them in the view? Like
this: @users[0]?

No, I make something like:

@users.each do |u|