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

Undefined method NoMethodError average #1

Open
railskipl opened this issue Feb 24, 2014 · 12 comments
Open

Undefined method NoMethodError average #1

railskipl opened this issue Feb 24, 2014 · 12 comments

Comments

@railskipl
Copy link

I am getting following error while rating
(undefined method `average' for #Mongoid::Criteria:0xb335e5b8)

   def update_rate_average(stars)
      if average.nil?
        RatingCache.create! do |avg|
          avg.cacheable_id = self.id
          avg.cacheable_type = self.class.name
          avg.avg = stars
          avg.qty = 1
        end
      else
        a = average
        a.qty = rates.count
        a.avg = rates.average(:stars) #error is here.. average method 
        a.save!(validate: false)
      end
    end

I am not getting, where average method is defined?

@malagant
Copy link
Owner

You’ll find that average is a relation defined in the concern: https://github.com/malagant/mongoid-letsrate/blob/master/lib/mongoid/letsrate/model.rb#L78

An 24. Februar 2014 at 08:33:31, railskipl ([email protected]://[email protected]) schrieb:

I am getting following error while rating
(undefined method `average' for #Mongoid::Criteria:0xb335e5b8)

def update_rate_average(stars)
if average.nil?
RatingCache.create! do |avg|
avg.cacheable_id = self.id
avg.cacheable_type = self.class.name
avg.avg = stars
avg.qty = 1
end
else
a = average
a.qty = rates.count
a.avg = rates.average(:stars) #error is here.. average method
a.save!(validate: false)
end
end

I am not getting, where average method is defined?


Reply to this email directly or view it on GitHubhttps://github.com//issues/1.

@railskipl
Copy link
Author

But still, I am not able to understand why it giving error.. at rates.average(:stars)

@malagant
Copy link
Owner

I checked in the rails console and can't reproduce the error? Which mongoid gem version do you use? How do you create the rating? Pleas provide more details and I will be happy to support you.

@malagant
Copy link
Owner

sorry, found the bug. Please wait for a fix.

@railskipl
Copy link
Author

thanks.. also I've encounter one issue :

 def can_rate?(user)
      user.ratings_given.where(rateable_id: id, rateable_type: self.class.name).size.zero?
 end

When I am inspecting user.ratings_given.where(rateable_id: id, rateable_type: self.class.name).size is returning me zero all the time, in which case can_rate? is always returning true even though user rated the model.

@malagant
Copy link
Owner

A fix for "average" is now in the repo and I released a new gem. I swear, that I will provide tests i the next release. ;)

@malagant
Copy link
Owner

For your second problem. The code works if you run it in the right context. E.g. I tried it in the rails console and found out, that the rateable_type could be Object in your example. If you use the right one, it returns correct values. Am I right?

@railskipl
Copy link
Author

First time when I rate it rates properly, but then again I rate ,it is not displaying rating. For example when I am rating 5 stars by [email protected](say) first time it rates properly then again I try to rate with 2 stars(same email) it should raise an error "User already rate" but it is not raising and it is not displaying the previous rating i.e. 5 stars also. Also I've tried the other way... I mean to say first I rate a car with [email protected] and then when I try to rate the same car with [email protected] I am not able to rate it when I refresh rating is gone for [email protected].
Also, the second issue which I've mentioned earlier it is still showing me zero all the time.

@railskipl
Copy link
Author

I've just now visited the rails console and found that when I rate first time say 2 stars avg. value in RatingCache is 2.0 but then again when I rate with another email with rating 5 stars, the avg. value is 0.0.

 RatingCache.all.to_a
 => [#<RatingCache _id: 530c35bccc79c22064000011, created_at: 2014-02-25 06:18:36 UTC, updated_at: 2014-02-25 06:18:36 UTC, avg: 1.0, qty: 1, cacheable_type: "Car", cacheable_field: nil, cacheable_id: "530c319ecc79c2012e000001">] 
1.9.3p448 :028 > RatingCache.all.to_a
 => [#<RatingCache _id: 530c35bccc79c22064000011, created_at: 2014-02-25 06:18:36 UTC, updated_at: 2014-02-25 06:19:05 UTC, avg: 0.0, qty: 2, cacheable_type: "Car", cacheable_field: nil, cacheable_id: "530c319ecc79c2012e000001">] 

@railskipl
Copy link
Author

I've found that rater_id in user model is nil. When I try to update rater_id from console it is raising an error
user.rater_id = "12345"
NameError: uninitialized constant Rater
Is it a reason for the second issue which I've mentioned yesterday. user.ratings_given.where(rateable_id: id, rateable_type: self.class.name).size

@railskipl
Copy link
Author

I've managed to find average as

    def update_rate_average(stars)
      if average.nil?
        RatingCache.create! do |avg|
          avg.cacheable_id = self.id
          avg.cacheable_type = self.class.name
          avg.avg = stars
          avg.qty = 1
        end
      else
        a = average
        str ||= []
        str << rates.map { |e| e.stars  }
        avg = str.flatten.inject(:+)/str.flatten.length #average
        a.qty = rates.count
        a.avg = avg
        a.save!(validate: false)
      end
    end

@malagant malagant reopened this Feb 27, 2014
@malagant
Copy link
Owner

I am currently writing the needed specs. I have to confess, that I did a quick and dirty write without any testing. I will provide something later this evening. Thank you for your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@malagant @railskipl and others