【问题标题】:undefined method `friendly' for #<ActiveRecord::Relation []>#<ActiveRecord::Relation []> 的未定义方法“友好”
【发布时间】:2013-12-16 02:05:24
【问题描述】:

我最近在我的 Rails 4 应用程序上安装了friendly_id 5。我遵循了快速入门指南,并设置了这样的模型:

class Official::Master < Official
  extend FriendlyId
  friendly_id :name, use: [:slugged, :history]

end

但是,当我尝试保存 Official::Master 时,出现此错误:

undefined method `friendly' for #<ActiveRecord::Relation []>

这发生在create 控制器操作中:

def create
  official = Official::Master.new(official_params)
  official.save # error occurs on this line
end

不幸的是,Rails 没有生成堆栈跟踪,而它通常会这样做。我不知道如何解决此错误。

更新:

移除了回溯消音器并得到了这个:

activerecord (4.0.1) lib/active_record/relation/delegation.rb:121:in method_missing' activerecord (4.0.1) lib/active_record/relation/delegation.rb:68:inmethod_missing' friendly_id (5.0.1) lib/friendly_id/slugged.rb:302:in scope_for_slug_generator' friendly_id (5.0.1) lib/friendly_id/history.rb:104:inscope_for_slug_generator' friendly_id (5.0.1) lib/friendly_id/slugged.rb:313:in slug_generator' friendly_id (5.0.1) lib/friendly_id/slugged.rb:294:inset_slug'

看起来错误出现在 slugged.rb 的第 302 行:

scope = scope.friendly unless friendly_id_config.uses? :finders

【问题讨论】:

    标签: ruby-on-rails friendly-id


    【解决方案1】:

    看起来friendlyid 5 还没有正确处理rails 4 中的单表继承。如果将下面的代码放到父类中会发生什么?

      extend FriendlyId
      friendly_id :name, use: [:slugged, :history]
    

    这里有一个或多或少解决相同问题的问题:Friendly_id 5 rc1 - single table inheritance issue

    【讨论】:

    • 谢谢,这绝对是这个错误的原因。不幸的是,这个解决方案在我的用例中不可行,因为官方固有的其他模型不应该有友好的 id。不过,很高兴知道发生了什么。
    • 实际上,您提供的链接似乎对如何处理这个问题有一些建议。一切都与该提示有关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 2013-08-06
    相关资源
    最近更新 更多