NoMethodError

Hello fellow Ruby enthusiasts!

Although I've been using Ruby for some time, I just started using RoR. I'm experiencing an error that I'm having trouble resolving and was hoping to find some help.

I am getting this error message when I try to hit a certain URL:

http://localhost:3000/captionfiles

NoMethodError in CaptionfilesController#index undefined method `all' for Captionfile:Class app/controllers/captionfiles_controller.rb:5:in `index'

Here are my files:

class CaptionfilesController < ApplicationController   def index     @captionfiles = Captionfile.all

    respond_to do |format|       format.html # index.html.erb       format.json { render json: @captionfiles }     end   end ............. class Captionfile < ActiveRecord::Base   validates :title, :presence => true, :length => { :minimum => 2 }   validates :filename, :presence => true   has_many :run end ............

Cccweb::Application.routes.draw do   resources :captionfiles   resources :runs   get "pages/choose"   get "home/index"   get "runs/new"   root :to => 'home#index' end .................

I've searched the internets as well as many message bases, and although I come up with variants of this message, none of the solutions seem to help.

Thank you!

Can you do anything useful with the Captionfile class? Creating records for example.

I assume you have you tried restarting the server.

Are you sure you have not got Captionfile declared in another file somewhere? Perhaps you kept a copy of captionfiles.rb with an out of date definition in it. Do a global source search for Captionfile in your project.

That should be has_many :runs by the way (plural) but I don't think that is anything to do with this problem.

Colin

There's nothing, at first glance, wrong with your code. So can you just confirm which version of Rails you have installed. Older (very old!) versions didn't have ".all", you had to use ".find(:all)". But if that's your problem, you must be on Rails 1.x, and that won't be the last of your problems...

Does it work when you run Captionfile.all in your rails console?

Hi Michael -- I am using Ruby v1.9.2p290, Rails v3.2.2, and rvm 1.8.5.

I checked to see if I had duplicate files, but to no avail. I will do some deep digging this morning. If I find a solution, I'll post it.

Thanks for your help.

Andy

Hi Colin,

I did try restarting the server, etc. I still can't fix the problem.

I am going to do some deep-diving into the code today. I suspect it has something to do with a file like routes.db, or similar. The code seems to be ok -- there's something else that isn't quite evident that's causing this issue.

Thanks for your help!

Andy

Hi Colin,

I did try restarting the server, etc. I still can't fix the problem.

You did not say whether you can do anything useful with the class at all (probably because you top posted rather than inserting comments inline). This would have told us whether it is something specific to do with #all or whether your class is not being recognised as an ActiveRecord class.

Colin

Hi Colin,

Thanks for the reply. Your question prompted me to search around my class declarations, and sure enough, I found the problem.

I declared two container classes, Captionfile and Run, to capture information in a convenient, encapsulated format. From there I create objects to make arrays/hashes for processing

I stored them in the /lib directory, which apparently conflicted with the ActiveRecord class declarations of the same name.

I renamed the class files and suddenly it all worked. I now have to figure out how to include these class files without conflict (I'll likely rename them).

Thanks for your tipoff! That's what lead me to my solution.

Cheers, Andy

Hi Colin,

Thanks for the reply. Your question prompted me to search around my class declarations, and sure enough, I found the problem.

I declared two container classes, Captionfile and Run, to capture information in a convenient, encapsulated format. From there I create objects to make arrays/hashes for processing

I stored them in the /lib directory, which apparently conflicted with the ActiveRecord class declarations of the same name.

That is why I suggested doing a global search for Captionfile.

I renamed the class files and suddenly it all worked. I now have to figure out how to include these class files without conflict (I'll likely rename them).

You can't have two different classes of the same name.

Thanks for your tipoff! That's what lead me to my solution.

Glad to be of help

Colin