Question

In a Rails 3 app, I am trying to write seeds (in db/seeds.rb) for a model that uses friendly_id:

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

When I run rake db:seed the task fails. Below is the gist when running with --trace:

rake aborted!
undefined local variable or method `friendly_id' for #<Class:0x007fa1de992ac8>
/Users/cornelius/.rvm/gems/ruby-1.9.2-p290@usg/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)>'
...

Here is the model:

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

I am using friendly_id as a Gem:

# /Gemfile
gem 'friendly_id'

Any help?

Was it helpful?

Solution

In your model you need to extend the class properly, the classname is FriendlyId, rather than friendly_id.

As a convention in Ruby and Rails, classnames are CamelCase and filenames are snake_case.

Your Page model should read:

class Page < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, use: :slugged
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top