Tony
Tony

Reputation: 10208

LoadError - Expected to define. Rails error when calling module

I have a module called EntityTrackerHelper. Here is the code:

module EntityTrackerHelper
    def self.createUserAction(user_id, type, entity_id)
        existingua = UserAction.find(:first, :conditions=> ["user_id = ? and type = ? and entity_id=?", user_id, type, entity_id])
        if existingua.nil?
            ua = UserAction.new
            ua.user_id = user_id
            ua.type = type
            ua.entity_id = entity_id
            ua.date = Time.now
            ua.save
        else
            existingua.date = Time.now
            existingua.save
        end
    end
end

It is used to track changes and user access in an entity. It is used in a controller as follows.

require "#{Rails.root}/lib/EntityTrackerHelper"
class LessonSectionsController < InheritedResources::Base
    def index
      user_id = params[:user_id]
      lesson_id = params[:lesson_id]
      EntityTrackerHelper::createUserAction(user_id, 'LESSON', lesson_id)
      lessonSections = LessonSection.find(:all, :conditions => { :lesson_id => params[:lesson_id] })
      render :json => {:sections => lessonSections.as_json({:only => [:lesson_id,:id,:name]}), :error => ''}
    end
end

I get the following error:

LoadError (Expected /<ProjPath>/<ProjName>/app/models/lesson.rb to define LESSON):
  lib/EntityTrackerHelper.rb:12:in `createUserAction'
  app/controllers/lesson_sections_controller.rb:9:in `index'

Line 12 in EntityTrackerHelper is UserAction.find...

Any idea?

Thanks

Upvotes: 0

Views: 4049

Answers (2)

tomferon
tomferon

Reputation: 5001

ActiveRecord will use the field type for "single table inheritance". See http://api.rubyonrails.org/classes/ActiveRecord/Base.html (subtitle: single table inheritance).

It means that when it loads the UserAction with type LESSON, ActiveRecord will try to instantiate the class LESSON which is not defined.

You probably should use another name for your type column.

Upvotes: 3

Thillai Narayanan
Thillai Narayanan

Reputation: 4896

You can try using

**include EntityTrackerHelper**

for more info check this link Call Module function from Controller (NoMethodError)

Upvotes: -1

Related Questions