From 699f3bcb8d741039271c5c96b76ca81e21e2cff3 Mon Sep 17 00:00:00 2001 From: brookzhang Date: Sat, 7 Dec 2013 10:31:28 +0800 Subject: [PATCH] can_rate? can not deal with dimension = nil --- lib/letsrate/model.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/letsrate/model.rb b/lib/letsrate/model.rb index 8dd319d..dd85247 100644 --- a/lib/letsrate/model.rb +++ b/lib/letsrate/model.rb @@ -42,7 +42,10 @@ def average(dimension=nil) end def can_rate?(user_id, dimension=nil) - val = self.connection.select_value("select count(*) as cnt from rates where rateable_id=#{self.id} and rateable_type='#{self.class.name}' and rater_id=#{user_id} and dimension='#{dimension}'").to_i + query = "select count(*) as cnt from rates where rateable_id=#{self.id} and rateable_type='#{self.class.name}' and rater_id=#{user_id} " + query += " and dimension='#{dimension}' " if dimension.present? + query += " and dimension is null " if dimension.nil? + val = self.connection.select_value(query).to_i if val == 0 true else