ahoy 和事件问题,无法自动加载错误

Issue with ahoy and the events, unable to autoload error

我刚刚尝试在控制台中查询事件,但收到以下错误消息:

`irb(main):003:0> Event
LoadError: Unable to autoload constant Event, expected /home/action/workspace/trendosaur/app/models/ahoy/event.rb to define it
        from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:481:in load_missing_constant'
        from /home/action/.gem/ruby/2.1.1/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:180:in const_missing'
        from (irb):3
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands/console.rb:90:in start'
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands/console.rb:9:in start'
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:69:in console'
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in run_command!'
        from /home/action/.gem/ruby/2.1.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in <top (required)>'
        from bin/rails:4:in require'
        from bin/rails:4:in <main>'`

这是预期的行为吗?

这是我的模型:

module Ahoy
  class Event < ActiveRecord::Base
    self.table_name = "ahoy_events"

    belongs_to :visit
    belongs_to :user
  end
end

和模式:

  create_table "ahoy_events", id: false, force: true do |t|
    t.uuid     "id",         null: false
    t.uuid     "visit_id"
    t.integer  "user_id"
    t.string   "name"
    t.json     "properties"
    t.datetime "time"
  end

  add_index "ahoy_events", ["time"], name: "index_ahoy_events_on_time", using: :btree
  add_index "ahoy_events", ["user_id"], name: "index_ahoy_events_on_user_id", using: :btree
  add_index "ahoy_events", ["visit_id"], name: "index_ahoy_events_on_visit_id", using: :btree

谢谢

如果仔细观察,您会发现 Event class 在 Ahoy 模块中。

当 class 位于模块内部时,您可以这样访问它:ModuleNmae::ClassNmae

所以在你的情况下:

Ahoy::Event

您可以正常使用它 class,例如:

Ahoy::Event.all
Ahoy::Event.where(name: "Viewed Book")

您为活动提供的其他属性可以在 properties 下找到。例如:

Ahoy::Event.where(name: "Viewed Book").first.properties