find_or_initialize_by 不适用于 2 列 [英] find_or_initialize_by doesn't work with 2 columns

查看:36
本文介绍了find_or_initialize_by 不适用于 2 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ActiveRecord 在我的数据库中插入数据.

I am trying to insert data in my database using ActiveRecord.

当我使用 pdays = MyModel.new 而不是下面的 find_or_initialize_by 进行初始化时,脚本运行良好.但它只运行一次.我需要它每天运行以执行更新.当我第二次尝试使用 pdays = MyModel.new 运行脚本时,不会引发重复的键约束.

When I use pdays = MyModel.new for initialization rather then the below find_or_initialize_by, the script runs fine. But it ONLY runs once. I need this to run daily to perform updates. When I try running the script the second time with pdays = MyModel.new, then no duplicate key constraint is raised.

因此,我正在尝试使用 2 个参数进行下面的 find_or_initialize_by,但这会出现错误:

Hence I am trying the below find_or_initialize_by with 2 arguments, but that gives an error:

未定义的方法`find_or_initialize_by'

undefined method `find_or_initialize_by'

两列一起构成唯一记录:

2 columns together make the unique record:

vertica_traffic.each do |vdays|

  pdays = MyModel.find_or_initialize_by([:local_dt], vdays[:community_id])

  pdays[:local_date]       = vdays_traffic[:local_d]
  pdays[:community_id]     = vdays_traffic[:community_id]
  pdays[:uniquesters]      = vdays_traffic[:uniques]

  pdays.save

end

推荐答案

你可以试试:

MyModel.find_or_initialize_by_local_dt_and_comunity_id([:local_dt], vdays[:community_id])

_and_

这篇关于find_or_initialize_by 不适用于 2 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆