【发布时间】:2016-07-16 10:49:42
【问题描述】:
通过使用 railscast 视频,我创建了一个适用于同一模型的简单搜索。我有日期、日期和时间字段。
我的模特
class Sale < ApplicationRecord
belongs_to :washer, optional: true
belongs_to :location, optional: true
has_many :sale_services
has_many :services, :through => :sale_services
def day
created_at.strftime('%A')
end
def time
created_at.strftime("%H:%M")
end
def date
created_at.strftime('%F')
end
def self.search(search)
if search
key = "'%#{search}%'"
columns = %w{ city station venue area country plate_number }
joins(:services).joins(:washer).joins(:location).where(columns.map {|c| "#{c} ILIKE #{key}" }.join(' OR '))
else
where(nil)
end
end
end
我需要更改什么以确保我可以在时间戳字段上搜索日期和日期?
【问题讨论】:
标签: ruby-on-rails ruby activerecord ruby-on-rails-5