2

This is what my Plan.rb looks like:

class Plan < ActiveRecord::Base

  has_many :users
  has_friendly_id :plan, :use_slug => true

end

User.rb

class User < ActiveRecord::Base

#There is other stuff in here, but I stripped it out for brevity purposes.

  has_and_belongs_to_many :projects
  has_many :stages
  has_many :uploads
  has_many :comments
  has_many :assignments
  has_many :roles, :through => :assignments
  belongs_to :plan


end

This is the error message I am getting:

'ruby-1.9.2-p0 > Plan.create(:name => "First_Test", :storage => "100")
NoMethodError: undefined method `plan' for #<Plan:0x00000102b8a2d8>
    /gemms/ruby-1.9.2-p0/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:364:in `method_missing'
    /gemms/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:46:in `method_missing'
    /gems/ruby-1.9.2-p0/gems/friendly_id-3.2.0/lib/friendly_id/slugged.rb:82:in `slug_text'
    /gems/ruby-1.9.2-p0/gems/friendly_id-3.2.0/lib/friendly_id/active_record_adapter/slugged_model.rb:49:in `build_a_slug'
    /gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:419:in `_run_save_callbacks'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/callbacks.rb:277:in `create_or_update'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/persistence.rb:39:in `save'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/validations.rb:43:in `save'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/attribute_methods/dirty.rb:21:in `save'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:237:in `block (2 levels) in save'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:237:in `block in save'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:248:in `rollback_active_record_state!'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:236:in `save'
    /gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/base.rb:498:in `create'
    from (irb):3
    /gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
    /gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
    /gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Not sure what I am doing wrong here, but I can't create any new 'plans'.

2 Answers 2

4

It looks like your database doesn't have a plan column in your plans table. In addition to adding the has_friendly_id statement to your model, did you create and run a migration to add the plan field to your database?

6
  • hrmm....a plan column to my plans table? Hrmm....something doesn't sound right with that. I actually don't have it, but why would I need a plan column in my plans table? Then I guess I should be referencing :plan_id ? Feb 1, 2011 at 6:18
  • Yep -- whatever you specify as has_friendly_id, there needs to be a database column with the same name. Feb 1, 2011 at 6:20
  • I added a plans table and a plan_id column to my user's table - if that's what you were referring to. But I am not sure why I would need a plan column in my plans table. Feb 1, 2011 at 6:20
  • Ahh...interesting. Hrmm....that's kinda strange. Ok, I will look into that. Thanks. Feb 1, 2011 at 6:21
  • Well if you're going to give something a friendly id, it's gotta be stored somewhere, right? If your :name field is unique, you may just want to do has_friendly_id :name instead? Feb 1, 2011 at 6:22
0

You can also get this error (undefined method `has_friendly_id') if you have upgraded friendly_id from Version 3 to 4.

Command 'has_friendly_id' has been replaced by 'friendly_id' - see how to upgrade here:

http://rubydoc.info/github/norman/friendly_id/master/file/WhatsNew.md

Note: You'll also need to create a migration to add a new slug column to your table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.