janida
janida

Reputation: 21

undefined method for model

I have been maintaining this system for almost 3 years now and the system is using rails 1.2.6 I have a problem when adding new attributes to the model. I get an undefined method for model error.

This worked previously but this time i could not fix it, even though when i run it in console, it is ok.

I have created 3 models with has_many :through relationship. I don't understand migrations well, so I have always created the tables manually.

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

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

when i tried to create new Casedf, I get this error:

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

Please help point something out. I've been looking at this for weeks.

Upvotes: 0

Views: 716

Answers (1)

DGM
DGM

Reputation: 26979

First of all, in rails, your models need to subclass from ActiveRecord. I hope you have made a typo in pasting. because you hav an extra space in your has_many calls

Upvotes: 2

Related Questions