2012-06-10 61 views
1

在Rails 3應用程序,我想寫種子(DB/seeds.rb)對於使用friendly_id模型:friendly_id,播種數據庫,未定義的方法 'friendly_id'

# /db/seeds.rb 
Page.create(:title => "Default page", :content => "Default content of the default page") 

當我運行rake db:seed任務失敗。下面是一個--trace運行時的要點:

rake aborted! 
undefined local variable or method `friendly_id' for #<Class:0x007fa1de992ac8> 
/Users/cornelius/.rvm/gems/[email protected]/gems/activerecord-3.2.5/lib/active_record/dynamic_matchers.rb:50:in `method_missing' 
/Users/cornelius/Sites/usg/app/models/page.rb:2:in `<class:Page>' 
/Users/cornelius/Sites/usg/app/models/page.rb:1:in `<top (required)>' 
... 

這裏是模型:

# /app/models/page.rb 
class Page < ActiveRecord::Base 
    extend friendly_id 
    friendly_id :title, use: :slugged 
end 

我使用friendly_id作爲寶石:

# /Gemfile 
gem 'friendly_id' 

任何幫助嗎?

+2

您是否嘗試過使用 「延長FriendlyId」? –

+0

該文檔(https://github.com/norman/friendly_id)建議它應該是'extend FriendlyId'而不是'extend friendly_id' - 但我不會寫這樣的答案並且竊取戴夫的代表。 – Shadwell

+2

@Shadwell別擔心, ipd將;) –

回答

2

在你的模型中,你需要正確地擴展類,類名是FriendlyId,而不是friendly_id。

作爲Ruby和Rails中的約定,類名是CamelCase,文件名是snake_case。

你的頁面模式應爲:

class Page < ActiveRecord::Base 
    extend FriendlyId 
    friendly_id :title, use: :slugged 
end