Debug 'env' in Rails initializer - ruby-on-rails

I want to check the value of env[:clearance] to see why something is broken.
How can I do this. I tried puts:
config.user.current = Proc.new { env[:clearance].current_user }
puts "ENV[:CLEARANCE]: #{env[:clearance]}"
but that gets: undefined local variable or methodenv' for main:Object (NameError)`
How can I get some visibility into the env hash in this situation? BTW this is loosely related to the Clearance gem.

It's not clear what problem you are trying to solve or what config.user.current is (it's not Clearance). One problem however is that you are trying to use the method env in the proc where it will not be defined. The proc has its own scope.
If you just want to see what ENV is after the clearance middleware has run the simplest thing to do would be to put a binding.pry or debugger statement inside a controller and access env that way.
The clearance environment is set by middleware in response to a request. It will not be set in an initializer.

Related

Unable to access ENV in Rails worker

I am trying to read an environment variable in my worker(also tried in rake tasks) like:
puts ENV('APILAYER_KEY')
but it throws the following error:
NoMethodError: undefined method `ENV' for #<ExchangeRateUpdater:0x0055c5a7a484f0>
As I can see ENV is supposed to be Ruby method and will be available everywhere. https://ruby-doc.org/core-2.2.0/ENV.html
Has anyone seen this problem?
Change the braces (as it's shown in the docs you've linked to):
ENV['APILAYER_KEY']
ENV is an object. The problem with your code is that Ruby treats it as if it was a method, and "APILAYER_KEY" as an argument passed to it.

Rails environment variables failing to load in model

I have an environment variable that I'm using in two places, one is in a rake task, the other is in a model method. When I call it from the rake task, everything is fine and the variable loads, but when I call it from the model it doesn't find anything. It's not a nil error or any other error, it just returns an empty string.
Is there any reason the environment variables would be overridden or inaccessible to the model?
It's being used to build a url:
http://#{AppEnv['env_var_1']}/this/is/#{AppEnv['env_var_2']}/a/path
--one more thing, myabe it's relevant, the model method is called after_create
EDIT:
thanks for the responses, but my question isn't how to access or use env vars, as you can see I'm already using them above. My question is why they are not loading in my model.
EDIT 2:
I have 4 relevant AppEnv variables, and [this is really weird so bear with me] when I check them on running the rake task (puts all 4 of them to the log), they are as expected. When I run the exact same class method, but called after_create in a model, 3 of the variables are empty, and one of them holds the value of a different variable. That is:
AppEnvVar1 is empty
AppEnvVar2 has the value of AppEnvVar4
AppEnvVar3 is empty
AppEnvVar4 is empty
If I change the method to self.method (allowing me to run it from the console), and run it, it works. So I'm pretty sure I've narrowed this down to an issue with the AppEnv during an after filter.
any ideas on where to dig?
Rails sets a global constant hash ENV that should be available anywhere in your app after it's initialized, including in your models. So you should be able to refer to any enviroment variable like this (assuming the relevant env variables has been set):
"http://#{ENV['ROOT_DOMAIN']}/this/is/#{ENV['SECONDARY_DOMAIN']}/a/path"
I restarted the server and everything worked fine. Mysterious...

Passing custom arguments into rails console (2.2.2)

I have a rails 2.2.2 app. I'd like to be able to start the console by passing in a custom variable, which i can then retrieve inside my environment. Something like
#i start console like so
rails/console production -uid 1182
Then, anywhere in my code, i would be able to access this "uid" variable in the same sort of way that i can access ENV['HOME'] or things like that. (it doesn't need to be in ENV, just so long as i can access it reliably).
Anyone know how i can do this? thanks, max
Try this:
uid=1182 rails/console production
This way you set env variable, accessible from Ruby with:
ENV['uid']

How to get the value of a Controller variable in rails console

I set a variable inside a Controller and I'm trying to just do something as simple as read that variable in the rails console.
I thought about just doing #test in the console which is the name of the variable. but it shows as >null. When I do puts under where I set the variable it traces out the correct value in my terminal window.
Any ideas what I need to do to get to this variable via the console.
I tried putting the name of the controller first and then .variable but that threw an error
I can see what's inside my models by just using the model name and some attributes like .first and .last
You'd have to instantiate the controller and provide a public accessor to get the value in rails console.
If you're trying to debug something, I recommend you check out Pry. It's a Ruby debugging REPL. Do a require 'pry' in your controller, and put binding.pry somewhere in an action, when you execute that controller method--either interactively in a browser, or via a functional test (I recommend the latter)--it will open the Pry REPL and #test will be in scope there.
Check out this Railscast for some help using it.
Alternately, just rely on good unit or functional testing. Write a test around the method and add an assertion on assigns(:#test) to compare the value to your expectation. Check out the RSpec controller spec documentation.

In Ruby on Rails, "if defined? Product" doesn't work in script/runner mode?

If there is a simple script and to distinguish whether it is running by itself or being run inside the Rails app environment, I tried using
if defined? Product
# something
end
but it failed to be recognized even though Product is defined and can be used otherwise. Since then I tried using
if defined? RAILS_ENV
instead and it works well, but wonder why the defined? Product doesn't work?
This should work
if Product
# something
end
defined? ModelName returns nil for all my models.
Loading development environment (Rails 2.3.8)
>> defined? Post
=> nil
But then if I do this
>> Post; defined? Post
=> "constant"
Probably because nothing is loaded until you touch it. Hope this helps.
Edit: Ah ok well then, script/runner is a non-interactive form of script/console, I would think it loads the whole Rails app and runs from that context. If you need to identify wether the call was made from script/runner I can only think of passing a parameter to the function Model.long_running_method(:runner => true) and do your conditional check on that or if that is not convenient enough set a ENV constant ENV['something_runner']. And do the condition check on that instead.

Resources