I am a newbie to RoR. I am trying to figure out how to check if a property is defined or not in the environment file(development.rb in this case).
We have a property defined in development.rb file, something like:
config.user = 'test-user'
user_name = (if Rails.application.config.user is available)?
Rails.application.config.user : 'some_other_value'
If there is a
config.user defined in every environment, but sometimes it has a value, and sometimes it doesn't, for example, it could be
nil or an empty string, you can use present?:
If it is not defined, you will get a
NoMethodError in the case above, so you can rescue it:
begin user_name = Rails.application.config.user.present? ? Rails.application.config.user : 'some_other_value' rescue NoMethodError user_name = 'some_other_value' end
respond_to? should also work, just make sure you don't confuse it with
respond_to, which is a Rails method. It might look something like this:
if Rails.application.config.respond_to?(:user) && Rails.application.config.user.present? user_name = Rails.application.config.user else user_name = 'some_other_value' end