In the show (html) page I have this
<% if @movie.views_count > 150 && @movie.ratings_chart_last_days(30) < 4 %>
You are in the first 3 positions
<% end %>
and in movie.rb I have this
def ratings_chart_last_days(number_of_days)
subset = Movie.where('movies.created_at >= ?', number_of_days.days.ago)
@ratings_chart_last_days ||= chart_position(:ratings_abs, subset)
end
But I have this report:
undefined method `<' for nil:NilClass
the ratings_chart_last_days method is returning nil. Then when it tries to do a < comparison it tries to execute the < method. NilClass doesn't support that method.
How to solve?!
EDITED
Chart_position code
module Chartable
def chart_position(attribute, start_query = nil)
attribute = self.class.connection.quote_column_name(attribute.to_s)
partition = partition_by(start_query || self.class.all, attribute)
self.class.from(partition, :s).select('s.id, s.position')
.find_by('s.id = ?', id).try(:position)
end
private
def partition_by(chain, attribute)
chain
.select('id, ROW_NUMBER() OVER ('\
"ORDER BY #{attribute} DESC, created_at DESC"\
') as position')
end
end
ratings_chart_last_daysnever returnsnil- mrzasa