0

私はフォーラムモンスターのデモに基づいた簡単なフォーラムに取り組んできました。私は現在、アプリケーションをRails3.0.9から3.2.11に更新しようとしています。アプリケーションをロードすると、Forum:Moduleの「スコープ」された未定義のメソッドが表示されます

Extracted source (around line #13):
10:         <%= link_to "Delete Category", category_path(category), :confirm => "Are you sure you want to delete this category?", :method => :delete if can? :manage, category %>
11:       </span>
12:     </div>
13:     <% if category.forums.size > 0 %>
14:     <div>
15:       <table>
16:         <tr class="smaller">

これが私のフォーラムモデルです

class Forum < ActiveRecord::Base

  # Associations
  has_many :topics, :dependent => :destroy
  has_many :posts, :through => :topics

  belongs_to :category

  # Accessors
  attr_accessible :title, :description, :state, :position, :category_id

  # Scopes
  default_scope :order => :'position ASC'

  # Validations
  validates :title,       :presence => true
  validates :description, :presence => true
  validates :category_id, :presence => true
end

これが私がコンソールをいじって学んだことです

ricky ~/forum $ rails console   
Loading development environment (Rails 3.2.11)
irb(main):001:0> Category.all.size
  Category Load (0.4ms)  SELECT `categories`.* FROM `categories` ORDER BY position ASC
=> 2
irb(main):002:0> Category.all.each do |cat|
irb(main):003:1* puts cat.forums.size
irb(main):004:1> end
  Category Load (0.6ms)  SELECT `categories`.* FROM `categories` ORDER BY position ASC
NoMethodError: undefined method `scoped' for Forum:Module
    from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations/association.rb:123:in `target_scope'
    from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations/association.rb:87:in `scoped'
    from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations/has_many_association.rb:41:in `count_records'
    from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:261:in `size'
    from /usr/lib/ruby/gems/1.9.1/gems/activerecord-3.2.11/lib/active_record/associations/collection_proxy.rb:46:in `size'
    from (irb):3:in `block in irb_binding'
    from (irb):2:in `each'
    from (irb):2
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):005:0> puts Forum.all.size
NoMethodError: undefined method `all' for Forum:Module
    from (irb):5
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
    from /usr/lib/ruby/gems/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):006:0>

この問題の性質についての刺激を事前に感謝します。

4

1 に答える 1

1

「フォーラム」モデル クラスは、config/application.rb のモジュールとまったく同じ名前です。

モデルまたはアプリケーションの名前を変更します。

于 2013-01-18T16:41:11.243 に答える