active-hash/active_hash

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

Closed this issue · 3 comments

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.

  • "ruby": "3.2.2"
  • "rails": "7.0.7.2"
  • "active_hash": "3.2.1"

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

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

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