Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can_rate? can not deal with dimension = nil #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/letsrate/model.rb
Expand Up @@ -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} "
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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
Expand Down