Rails acts-as-dag gem安装配置及ArgumentError scope报错问题求助
问题原因
- 模型配置错误:
Learn::Graph::Node模型中存在多余的方法调用,acts_as_dag_links是边模型专属方法,节点模型仅需调用has_dag_links即可,重复调用会触发逻辑冲突。 - 版本兼容问题:你使用的 Rails 5.2+ 要求所有 ActiveRecord scope 定义必须为可调用对象(lambda/proc 包裹),而你引入的
acts-as-daggem 长期未更新,内部仍使用旧版无 lambda 包裹的 scope 写法,也会触发该报错。
解决方案
- 首先修正模型配置:
Learn::Graph::Node模型修正后代码如下:
class Learn::Graph::Node < ApplicationRecord has_dag_links :link_class_name => 'Learn::Graph::Edge' end
Learn::Graph::Edge 模型配置无需修改,保持原有即可:
class Learn::Graph::Edge < ApplicationRecord acts_as_dag_links :node_class_name => 'Learn::Graph::Node' end
- 若修正模型后仍报错,处理 gem 兼容问题:
- 方案一:手动给
acts-as-daggem 打补丁,将其内部所有 scope 定义修改为 lambda 包裹形式,示例修改逻辑:原写法scope :xxx, {where(xxx: yyy)}改为scope :xxx, -> { where(xxx: yyy) } - 方案二:若项目版本允许,将 Rails 降级到 5.1 及以下版本,旧版 Rails 允许 scope 直接传入哈希条件。
验证方法
修改完成后重启 Rails 控制台,执行 Learn::Graph::Node.first 即可验证是否修复。
内容的提问来源于stack exchange,提问作者yogeshmanjhi
相关产品推荐
相关产品推荐

