我有一个Rails5应用程序,想分配一些日期(而不是日期时间)列的广告模型。
模型架构如下所示,包括两个日期列-“start”和“end”。
create_table "adverts", force: :cascade do |t|
t.integer "user_id"
t.integer "product_id"
t.date "start"
t.date "end"
t.boolean "expired", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
广告控制器还允许更新这些列,
def advert_params
params.require(:advert).permit(:user_id, :product_id, :start, :end, :expired)
end
然后我在控制器中有一个方法来更新广告。
路线是
post 'update_advert', :on => :collection
使用find\u或\u initialize提取/创建广告记录。
def update_advert
@advert = Advert.find_or_initialize_by(user_id: advert_params[:user_id], product_id: advert_params[:product_id])
logger.info "checkpoint1"
logger.info advert_params.inspect
logger.info "checkpoint2"
...
end
I, [2020-05-19T12:32:46.947732 #13110] INFO -- : [e741ba19] checkpoint1
I, [2020-05-19T12:32:46.948986 #13110] INFO -- : [e741ba19] <ActionController::Parameters {"user_id"=>1, "product_id"=>1000, "expired"=>false} permitted: true>
I, [2020-05-19T12:32:46.949151 #13110] INFO -- : [e741ba19] checkpoint2
注意,缺少start和end列,因此无法继续更新它们。
如果我通过标准railsweb界面“显示”记录,我会看到预期的字段。
如果我通过jsonapi提取记录,就会看到预期的字段。
{"id":4,"user_id":1,"product_id":1000,"start":"2020-05-18","end":"2020-06-18","expired":false}
我的问题是-为什么在find\u或\u initialize响应中不返回start和end列?
谢谢你的阅读。