Even though Rails.application.config.trackable_models
returns a full array, defined?(Rails.application.config.trackable_models)
returns nil.
Rails.application.config.trackable_models # => ["NewsItem", "ContentPage", "Event"]
defined?(Rails.application.config.trackable_models): # => nil
Setting a local variable in the same way is fine:
foo = ["x"]
defined?(foo) # => local-variable
What is the proper way to check for the existence of a config variable if not "defined?"?
If you want to check the method is defined in the literal sense, use respond_to
Rails.application.config.respond_to?(:trackable_models)
If you want to check if the method returns something other than nil
, you have some options:
tm = Rails.application.config.trackable_models
tm.nil?
tm.presence || 'default value'
tm.present?
tm.blank?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With