ruby-on-rails 如何在Rails中创建没有字符串的查询

hvvq6cgz  于 7个月前  发布在  Ruby
关注(0)|答案(2)|浏览(86)

我有一个查询,

project.active.where("delivery_date <= :date", date: Date.today)

字符串
我不想使用这样的字符串

project.active.where(delivery_date: delivery_date <= Date.today)


但不工作,我得到了这个错误:

syntax error, unexpected ')', expecting `end'

ebdffaop

ebdffaop1#

你可以使用Arel,但你在这里没有得到任何东西,如果有的话,它使它更难阅读:

Project.active.where(
  Project.arel_table[:delivery_date].lteq(Date.today)
)

字符串

xxb16uws

xxb16uws2#

where(delivery_date: Date.new..Date.today)

字符串
或者Date.new可以替换为server/db的开头。你也可以这样做,但是这只是一个很长的范围。为什么你不想使用字符串呢?

相关问题