如何修复未定义的方法`find_by_friendly_id'

时间:2017-08-04 14:01:53

标签: ruby-on-rails permalinks slug friendly-id

我正在尝试这个

Opportunity.find_by_friendly_id(params[:id])

在配置了friendly_id的rails应用程序中。我得到了

undefined method `find_by_friendly_id' for #<Class:0x007f8060395ca0> Did you mean? find_by_sql

有趣的是,这有效:

Opportunity.friendly.find(params[:id])

怎么回事?

这里是来自https://github.com/norman/friendly_id/blob/master/lib/friendly_id/finder_methods.rb

的文档
# If you want to search only by the friendly id, use {#find_by_friendly_id}.
# @raise ActiveRecord::RecordNotFound

更新:有人想看到这个:

$ cat components/core/app/models/opportunity.rb | grep friendly
  friendly_id :slug_candidates, use: :slugged
$

1 个答案:

答案 0 :(得分:0)

我最终将finders添加到模型

friendly_id :foo, :use => [:slugged, :finders]