我有一个活动的记录查询在控制台中运行正常但在我在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
答案 0 :(得分:1)
好吧,猜错了: -
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
时会将结果导入
错误:函数计数(整数,字符变化,字符变化) 不存在
表示Postgres does not support count()with more than one column
所以解决方法是,如果size()
如此,您可以使用count()
: -
attendees_with_sms_plans.size
答案 1 :(得分:0)
请使用以下查询检查功能计数是否存在
SELECT n.nspname
FROM pg_extension e
JOIN pg_namespace n
ON e.extnamespace = n.oid
WHERE e.extname = 'count';
如果不在search_path
上,则您的查询将无法找到该功能。
要在不同的架构中使用扩展,请在以下示例中公开,删除并重新创建它:
DROP EXTENSION count;
CREATE EXTENSION count SCHEMA public;