2013-05-14 40 views

回答

2

什麼如果,例如,你的模型是用戶,找到您要使用的所有用戶:

User.all 

和特定的條件下,你可以使用:

​​
2

您可以使用where

Foo.where(:stuff => stuff, :thing => thing) 
# or 
Foo.where("stuff = ? AND thing = ?", stuff, thing) 

另見Rails .where vs .find ...

要查找所有(再次作爲ActiveRecord::Relation對象,而不是一個數組),你可以使用scoped

Foo.scoped 
+0

不串起到什麼功能? – Btuman 2013-05-14 18:01:13

+0

@Btuman它是與SQL注入作鬥爭(在[docs](http://guides.rubyonrails.org/active_record_querying.html#conditions)中推薦)。 – 2013-05-14 18:03:07

+0

我想.where()是將條件=> {}轉換爲當前的格式,我錯過了什麼嗎? – Btuman 2013-05-14 18:06:10