gpt4 book ai didi

ruby-on-rails - 我如何获得在 rake 任务中工作的 Rails 控制台中工作的 Active Record 查询?

转载 作者:行者123 更新时间:2023-11-29 14:31:54 24 4
gpt4 key购买 nike

我有一个事件记录查询,它在控制台中运行良好,但在 rake 任务中使用时却不起作用

这是我的 rake 任务的开始:

namespace :attendees do
desc "Migrate sms plans to receive_sms attribute for attendees"
task migrate_sms_plans: :environment do
attendees_with_sms_plans = Attendee.select('attendees.id, attendees.phone, attendees.local_phone').joins(:plans).where('plans.name = ?', "Yes! SMS!")

为什么 select 语句在我的 Rails 控制台中有效,但在 rake 任务中使用它时出现以下错误,我该如何解决:

ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  function count(integer, character varying, character varying) does not exist
LINE 1: SELECT COUNT(attendees.id, attendees.phone, attendees.local_...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

原来错误不是select语句,而是在下一行:

puts <<-TEXT
Going to migrate #{attendees_with_sms_plans.count} attendees from "Yes! SMS!" plan to receive_sms: true attribute on Attendee Model
TEXT

我修改为:

puts <<-TEXT
Going to migrate #{attendees_with_sms_plans.to_a.count} attendees from "Yes! SMS!" plan to receive_sms: true attribute on Attendee Model
TEXT

最佳答案

好吧,到目前为止猜错了:-

attendees_with_sms_plans = Attendee.select('attendees.id, attendees.phone, attendees.local_phone').joins(:plans).where('plans.name = ?', "Yes! SMS!")

attendees_with_sms_plans 的结果为 Attendee::ActiveRecord_Relation

并调用 attendees_with_sms_plans.count 将结果放入

ERROR: function count(integer, character varying, character varying) does not exist

这意味着Postgres does not support count()with more than one column

所以解决方案是,如果 count() 像这样,您可以使用 size() 代替:-

attendees_with_sms_plans.size

关于ruby-on-rails - 我如何获得在 rake 任务中工作的 Rails 控制台中工作的 Active Record 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50478998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com