Ruby LoadError-应定义。调用模块时发生Rails错误
我有一个叫做EntityTrackerHelper的模块。代码如下:Ruby LoadError-应定义。调用模块时发生Rails错误,ruby,ruby-on-rails-3,module,Ruby,Ruby On Rails 3,Module,我有一个叫做EntityTrackerHelper的模块。代码如下: 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]
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
需要“#{Rails.root}/lib/EntityTrackerHelper”
类LessonSectionsController{:lesson\u id=>params[:lesson\u id]})
呈现:json=>{:sections=>lessonSections.as_json({:only=>[:lesson_id,:id,:name]}),: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'
LoadError(需要///app/models/lesson.rb来定义课程):
lib/EntityTrackerHelper.rb:12:in'createUserAction'
app/controllers/lesson_sections_controller.rb:9:in'index'
EntityTrackerHelper
中的第12行是UserAction.find…
有什么想法吗
谢谢您可以尝试使用
**include EntityTrackerHelper**
有关更多信息,请检查此链接ActiveRecord将使用字段
类型
进行“单表继承”。请参阅(副标题:单表继承)
这意味着当ActiveRecord加载类型为LESSON
的UserAction
时,它将尝试实例化未定义的类LESSON
您可能应该为您的
类型列使用另一个名称。可能模型的代码会有用。哪一个?我不知道;I don’我不知道为什么错误出现在lesson.rb中,所涉及的模型是lesson部分(不是lesson)。错误涉及app/models/lesson.rb
。无论如何,请看我的答案,我想这就是它在这个文件中搜索课程的原因。谢谢!这就是问题所在。我将不再使用名为type的列。