can_rate? can not deal with dimension = nil #51

Open
wants to merge 1 commit into
from

Projects

None yet

2 participants

@brookzhang

If dimension.nil? , query should be " dimension is null ", not "dimension = '' ".

@3den 3den commented on the diff Jan 9, 2014
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} "
@3den
3den Jan 9, 2014

both the old version and this one don't work with inheritance because the polymorphic association will use the class.parent_name instead of class.name

@3den
3den Jan 9, 2014

a better approach would be let active record help us, something like:

  val = rates.select_value("count(*) as cnt").where(dimension: dimension, rater_id: user_id)
@3den
3den Jan 9, 2014

this code is much simpler and will work find with model inheritance

@wazery wazery referenced this pull request in wazery/ratyrate Jul 30, 2014
Open

can_rate? can not deal with dimension = nil #23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment