看似简单的问题。生成ActiveRecord :: Relation没有任何方法或使用`all()`
我试图从模型中构建一个ActiveRecord::Relation
对象,而不使用像where()
这样的方法。例如:
@people = Person
@people.where(status: 'active') if params(:active)
@people.where(is_smoker: true) if params(:smokers)
return @people
你可以看到,如果没有active
或smokers
在params为设定,@people仅仅是模型,而不是ActiveRecord::Relation
。
我可以在return @people
的末尾输入all
,但必须有更好的方法。
想法?
最快的答案。布拉沃和谢谢。 –
你回答得太快了,我甚至不能再接受3分钟。 –
啊哈这是一个简单的问题,如果我想得到代表,我必须快速回答! – MrYoshiji