active-hash / active_hash

A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource
MIT License
1.19k stars 178 forks source link

undefined method `find_by_key' for ActiveYamlBaseClass:Class Did you mean? find_by_id #294

Closed chikugoy closed 8 months ago

chikugoy commented 9 months ago

The following error may occur

undefined method `find_by_key' for ActiveYamlBaseClass:Class Did you mean? find_by_id

The code for the target error class is as follows

class ActiveYamlBaseClass < ActiveYaml::Base
  set_root_path Rails.root.join('config/yaml')
  set_filename 'test_events'

  class << self
    def targets(event_key)
      event = ActiveYamlBaseClass.find_by_key(event_key)
      raise ArgumentError

      event.targets
    end
  end
end

Calling the above class as follows will result in an error

ActiveYamlBaseClass.targets(event_key)

Errors are rare; most are handled successfully.

flavorjones commented 8 months ago

I cannot reproduce this issue when the yaml file contains records with a key key. Please double-check your data file, and if you think it's formed properly, please provide it here to help us reproduce! Thanks.

kbrock commented 8 months ago

Are you able to rewrite as find_by(key: event_key)? That is the suggested rails way to lookup by a key.

flavorjones commented 8 months ago

Closing, but happy to reopen if @chikugoy is able to provide more information.