2011-12-27 53 views
0

我有一個名爲EntityTrackerHelper的模塊。以下是代碼:LoadError - 預計會定義。調用模塊時Rails錯誤

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 

它用於跟蹤實體中的更改和用戶訪問。 它在控制器中使用如下。

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 

我得到以下錯誤:

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' 

線12 EntityTrackerHelperUserAction.find...

任何想法?

感謝

+0

也許模型的代碼會很有用。 – thoferon 2011-12-27 15:50:18

+0

哪一個?我不知道爲什麼錯誤是在lesson.rb中,並且涉及的模型是LessonSection(不是課程) – Tony 2011-12-27 15:58:42

+0

錯誤提到'app/models/lesson.rb'。無論如何,看到我的答案,我想這就是它在這個文件中搜索'LESSON'的原因。 – thoferon 2011-12-27 16:01:37

回答

3

的ActiveRecord將使用領域type爲 「單表繼承」。請參閱http://api.rubyonrails.org/classes/ActiveRecord/Base.html(副標題:單表繼承)。

這意味着當它加載類型爲LESSONUserAction時,ActiveRecord會嘗試實例化沒有定義的類LESSON

您可能應該爲您的type列使用另一個名稱。

+0

謝謝!這是問題所在。我永遠不會再使用一個名爲type的列。 – Tony 2011-12-27 16:14:20

相關問題