我一直在开发一个基于论坛怪物演示的简单论坛。我目前正在尝试将应用程序从 rails 3.0.9 更新到 3.2.11。当我加载应用程序时,我得到一个未定义的方法 `scoped' for 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>
提前感谢任何关于此问题性质的煽动。