未定义的模型方法

时间:2012-02-27 03:11:12

标签: ruby-on-rails ruby

我一直在维护这个系统近3年,系统正在使用rails 1.2.6 向模型添加新属性时遇到问题。我得到一个未定义的模型错误方法。

之前有效,但这次我无法修复它,即使我在控制台中运行它也没关系。

我用has_many创建了3个模型:通过关系。我不太了解迁移,所以我总是手动创建表。

class Casedf < ActiveRecord::Base
 has_many :casespus
 has_many :spucats, :through => :casespus
end

class Spucat < ActiveRecord::Base
 has_many :casedfs, :through => :casespus
 has_many :casespus
end

class Casespu < ActiveRecord::Base
 belongs_to :casedf
 belongs_to :spucat
end

我的new.rhtml

 <td>Category<img src="/main/images/asterick1.gif" height="10" width="10" alt=""></td>
<td>:</td>
<td><select name="case[spucats][]" multiple>
         <option value="" selected="selected">Please Select</option>
             <% @spus.each do |category|%>
         <option value="<%= category.id%>">
             <%= category.name%> </option>
         <%end%>
    </select>
</td>

当我尝试创建新的Casedf时,我收到此错误:

Undefined method 'spucats=' for #<Casedf:....>
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:1860:in `method_missing'       
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:1675:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:1675:in `attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:1674:in `each'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:1674:in `attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/base.rb:1508:in `initialize_without_callbacks'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active_record/callbacks.rb:225:in `initialize'
/home/jaz/dfp/app/controllers/casedf_controller.rb:89:in `new'
/home/jaz/dfp/app/controllers/casedf_controller.rb:89:in `createcase'
/usr/bin/mongrel_rails:19:in `load'
/usr/bin/mongrel_rails:19

请帮忙指出一些事情。我已经看了几个星期。

1 个答案:

答案 0 :(得分:2)

首先,在rails中,您的模型需要从ActiveRecord继承。我希望你在粘贴时写错了。因为你在has_many电话中有额外的空间