Deekor
Deekor

Reputation: 9499

undefined method `size' for nil:NilClass

Why is my code returning this error?

@articles = Article.order("id DESC").where(:visible => 1)
if @aritcles.size > 15
  @articles = Article.order("id DESC").where(:visible => 1).limit(15)
end

returns:

undefined method `size' for nil:NilClass

If i run

@articles = Article.order("id DESC").where(:visible => 1)
@articles.size

It returns an integer...

Upvotes: 0

Views: 6634

Answers (3)

Ganesh Kunwar
Ganesh Kunwar

Reputation: 2653

Replace
@aritcles.size > 15
By
@articles.seze > 15

Upvotes: 0

xdazz
xdazz

Reputation: 160923

if @aritcles.size > 15 should be if @articles.size > 15.

But your code is odd, it is not necessary to do that. Just do the below is enough.

@articles = Article.order("id DESC").where(:visible => 1).limit(15)

Upvotes: 3

Matzi
Matzi

Reputation: 13925

If that is from your real code, then you mispelled articles to aritcles.

If that is different, please provide the real code.

Upvotes: 3

Related Questions