ユーザーモデルがあり、ID が 100 未満のすべてのユーザーを表示する必要があります。私のコードは
User.where(:id < 100)
エラーメッセージは「シンボルと 100 の比較に失敗しました」です。ID が 100 より小さいすべてのユーザーを取得するにはどうすればよいですか? 100 から 200 の間の ID が必要な場合はどうなりますか?
ユーザーモデルがあり、ID が 100 未満のすべてのユーザーを表示する必要があります。私のコードは
User.where(:id < 100)
エラーメッセージは「シンボルと 100 の比較に失敗しました」です。ID が 100 より小さいすべてのユーザーを取得するにはどうすればよいですか? 100 から 200 の間の ID が必要な場合はどうなりますか?
SQLまたは@Moriが提案するような追加のgemを避けたい場合は、次のことができます
User.where(:id => 1..100)
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}