一定时间后如何自动更改属性值

时间:2020-04-28 02:48:20

标签: ruby-on-rails ruby

我当前正在创建一个拍卖应用程序,我很难确定如何在特定时间更改拍卖价值。这是拍卖课的模型

#  id                 :bigint           not null, primary key
#  active             :integer          default(1)
#  auction_end_time   :datetime
#  auction_start_time :datetime
#  category           :string
#  current_price      :float
#  description        :text
#  highest_bid        :float
#  highest_bidder     :string
#  name               :string
#  seller             :string
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  user_id            :bigint

当auction_end_time经过当前时间(Time.new.utc)时,我想将'active'属性更改为'0'的值,以设置为false。对于“ true”和“ false”,“ active”限制为0和1。

1 个答案:

答案 0 :(得分:0)

active仅具有两个状态– truefalse并且该状态仅取决于auction_end_time是否已经通过时,则根本不需要该列

要查找所有active的拍卖,您可以使用以下范围:

scope :active, -> { where('auction_end_time > ?', Time.new.utc)

并查询拍卖是否仍为active,可以使用以下方法:

def active?
  auction_end_time > Time.new.utc
end

其他方法(例如CRON作业或后台工作者)将不太可靠,因为如果底层系统出现故障,它们可能会运行几秒钟甚至太晚甚至根本无法运行。