0

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

`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

谢谢

4

1 回答 1

0

如果您仔细观察,您会发现Event该类位于Ahoy模块内部。

当一个类在一个模块中时,你可以这样访问它:ModuleNmae::ClassNmae

所以在你的情况下:

Ahoy::Event

您可以将其用作普通类,例如:

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

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

Ahoy::Event.where(name: "Viewed Book").first.properties
于 2015-09-10T15:00:09.420 回答