【问题标题】:Undefined method for friendly_id implementationfriendly_id 实现的未定义方法
【发布时间】:2011-02-01 06:12:49
【问题描述】:

这就是我的 Plan.rb 的样子:

class Plan < ActiveRecord::Base

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

end

用户.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

这是我收到的错误消息:

'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>'

不确定我在这里做错了什么,但我无法创建任何新的“计划”。

【问题讨论】:

    标签: ruby-on-rails-3 friendly-id


    【解决方案1】:

    您的数据库在您的plans 表中似乎没有plan 列。除了将has_friendly_id 语句添加到您的模型之外,您是否创建并运行迁移以将plan 字段添加到您的数据库?

    【讨论】:

    • hrmm....将plan 列添加到我的plans 表?嗯....听起来有些不对劲。我实际上没有,但为什么我的plans 表中需要一个plan 列?那我想我应该引用:plan_id
    • 是的——无论您指定为has_friendly_id,都需要有一个同名的数据库列。
    • 我在我的用户表中添加了一个 plans 表和一个 plan_id 列 - 如果这就是您所指的。但我不确定为什么我的plans 表中需要一个plan 列。
    • 啊……有趣。嗯……这有点奇怪。好的,我会调查的。谢谢。
    • 好吧,如果你要给某个东西一个友好的 id,它必须存储在某个地方,对吧?如果您的:name 字段是唯一的,您可能只想改用has_friendly_id :name
    【解决方案2】:

    如果您已将friendly_id 从版本 3 升级到 4,您也可能会收到此错误(未定义的方法 `has_friendly_id')。

    命令 'has_friendly_id' 已替换为 'friendly_id' - 在这里查看如何升级:

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

    注意:您还需要创建迁移以向表中添加新的 slug 列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      相关资源
      最近更新 更多