2012-09-21 101 views
0

当用这些参数运行下面的代码时,我总是收到一个错误undefined method whereRails in In不知道在哪里

任何人都可以看到我做错了什么?

感谢

Parameters: {"category_ids"=>["1", "3", "4"]} 

    @pieces = Piece.all 
    @pieces = @pieces.where(:category_id => params[:category_ids]) if params[:category_ids].present? 
    @pieces = @pieces.where(:brand_id => params[:brand_ids]) if params[:brand_ids].present? 
    @pieces = @pieces.where(:color_id => params[:color_ids]) if params[:color_ids].present? 
    @pieces = @pieces.where(:user_id => params[:friend_ids]) if params[:friend_ids].present? 

回答

0

它应该是:

Piece.where(:category_id => params[:category_ids]) if params[:category_ids].present?

+0

即使它是通过过滤所有这些过滤器?也许我应该使用:条件? - 谢谢顺便说一下:) – Dol

+0

或者你的意思仅仅是第一@ piece.where应该是Piece.where? – Dol

+0

Piece.all不返回给你一个ActiveRelation。这意味着,你不能在其上运行“where”。相反,所有Piece.all都会返回一个Piece对象数组。 –

1

User.all回报你的user objectsArray所以在使用上where给出了一个错误NoMethodError:未定义的方法`其中” Array

所以使用下面的inst EAD

@pieces = Piece.where(:category_id => params[:category_ids]) 
+0

感谢您的解释 – Dol