ruby-on-railsahoy

Issue with ahoy and the events, unable to autoload error


I have just tried to query Events in console, and I get the following error message:

`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>'`

Is this expected behavior?

Here is my model:

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

    belongs_to :visit
    belongs_to :user
  end
end

And Schema:

  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

Thanks


Solution

  • If you look closer you can see that the Event class is inside Ahoy module.

    When a class is inside a module, you can access it that way: ModuleNmae::ClassNmae

    so in your case:

    Ahoy::Event
    

    and you can use that as normal class, for example:

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

    other properties you gave the event can be found under properties. for example:

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