0

ユーザーモデルがあり、ID が 100 未満のすべてのユーザーを表示する必要があります。私のコードは

User.where(:id < 100)

エラーメッセージは「シンボルと 100 の比較に失敗しました」です。ID が 100 より小さいすべてのユーザーを取得するにはどうすればよいですか? 100 から 200 の間の ID が必要な場合はどうなりますか?

4

2 に答える 2

2

SQLまたは@Moriが提案するような追加のgemを避けたい場合は、次のことができます

User.where(:id => 1..100)
于 2012-09-24T14:22:54.817 に答える
1

You need to drop into a SQL where clause for that:

User.where("id < 100")

But there's a gem that will let you do what you're trying to do: squeel. With that installed you could get the same result with:

User.where{id < 100}

For the range test:

User.where("id > 100 and id < 200")

or with squeel:

User.where{id > 100 and id < 200}
于 2012-09-24T14:15:51.127 に答える