ruby - Why does `where` give an "undefined method" error in Rails? -
i'm trying feedbacks specific attribute. using code:
def index feedbacks = feedback.all if params[:tag] @average_customer_rating = feedbacks.where('buyer_feedback_date not null').rated(feedback::from_buyers).average(:buyer_rating) || 0 @products = product.includes(:images).tagged_with(params[:tag]).order('desc').limit(22) else @products = product.includes(:images).all @average_customer_rating = feedbacks.where('buyer_feedback_date not null').rated(feedback::from_buyers).average(:buyer_rating) || 0 end end
and rails shows error:
undefined method `where' []:array
why can't use where
here , how fix it?
in rails 3.x, all
returns array:
feedbacks = feedback.all # <- array feedbacks.where(...) # <- fails
to activerecord::relation
, have use scoped
:
feedbacks = feedback.scoped # <- activerecord::relation feedbacks.where(...) # <- works
see working scopes more examples.
note distinction not needed in rails 4 anymore – scoped
deprecated in favor of all
returns activerecord::relation
.
Comments
Post a Comment