我最近在我的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:in
的method_missing” friendly_id(5.0.1)lib / friendly_id / slugged.rb:302:inscope_for_slug_generator' friendly_id (5.0.1) lib/friendly_id/history.rb:104:in
scope_for_slug_generator” friendly_id(5.0.1)lib / friendly_id / slugged.rb:313:inslug_generator' friendly_id (5.0.1) lib/friendly_id/slugged.rb:294:in
set_slug'
看起来错误发生在slugged.rb的第302行:
scope = scope.friendly unless friendly_id_config.uses? :finders
答案 0 :(得分:2)
似乎friendlyid 5还没有正确处理rails 4中的单表继承。如果将下面的代码放到父类中会发生什么?
extend FriendlyId
friendly_id :name, use: [:slugged, :history]
这是一个解决或多或少相同问题的问题:Friendly_id 5 rc1 - single table inheritance issue