Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a Rails.application.config variable is defined?

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?"?

like image 821
Alien Life Form Avatar asked Oct 19 '25 03:10

Alien Life Form


1 Answers

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?
like image 117
steel Avatar answered Oct 21 '25 16:10

steel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!