web-dev-qa-db-ja.com

Rails 3検索で日付を比較するよりも大きくできますか?

この検索はRails 3にあります:

Note.where(:user_id => current_user.id, :notetype => p[:note_type], :date => p[:date]).order('date ASC, created_at ASC')

しかし、:date => p[:date]条件は:date > p[:date]と同等である必要があります。これどうやってするの?読んでくれてありがとう。

122
ben
Note.
  where(:user_id => current_user.id, :notetype => p[:note_type]).
  where("date > ?", p[:date]).
  order('date ASC, created_at ASC')

または、すべてをSQL表記に変換することもできます

Note.
  where("user_id = ? AND notetype = ? AND date > ?", current_user.id, p[:note_type], p[:date]).
  order('date ASC, created_at ASC')
223
Simone Carletti

列名があいまいな問題に遭遇した場合、次のことができます。

date_field = Note.arel_table[:date]
Note.where(user_id: current_user.id, notetype: p[:note_type]).
     where(date_field.gt(p[:date])).
     order(date_field.asc(), Note.arel_table[:created_at].asc())
68
Sarah Vessels

あなたが使用しようとすることができます:

where(date: p[:date]..Float::INFINITY)

sQLで同等

WHERE (`date` >= p[:date])

結果は次のとおりです。

Note.where(user_id: current_user.id, notetype: p[:note_type], date: p[:date]..Float::INFINITY).order(:fecha, :created_at)

そして私も変わった

order('date ASC, created_at ASC')

For

order(:fecha, :created_at)
1
sesperanto