mardi 18 avril 2017

Rails How make a conditional inside a method on the model

I'm trying to make that just the user can feedback on a feedback method in the feedback model. but in the logs shows

NoMethodError (undefined method `total_feedbacks' for nil:NilClass)

so show to make a conditional in the model?

 def feedback_product

   user = User.find_by_id(attributes['user_id'])
  if user
    product.total_feedbacks += 1

    product.average_rating = product.feedbacks.where('buyer_feedback_date IS NOT NULL').rated(Feedback::FROM_BUYERS).average(:buyer_rating)

    product.save

  end

 end

the feedback belongs to user

 belongs_to :user

and user has_many :feedbacks

Aucun commentaire:

Enregistrer un commentaire