Rails environment variables failing to load in model - ruby-on-rails

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...

Related

data driven ios-Calabash automated testing using xml or css or global variables

i am doing automated testing using calabash-ios. I want to be able to run cucumber once and have it run x times for x user names and run through the gamut of test scenarios.
i want to use this:
Given I login as [#{country-name}] user using id [#{Login-name}] and pwd "PASSWORD"
and have a global variable that can store the values for both country and user name.
i had hoped to use scripts to run cucumber x times and set the value for the global variables each time. is this possible? and if so, could someone point me in the right direction?
i tried using :
##Loginname=value
but got this error:
features/step_definitions/common.rb:1: warning: class variable access from toplevel
uninitialized class variable ##Login in Object (NameError)
failing which, will it be possible to access data stored in a xml or css file using calabash?
If you want to run the same cucumber run many times with some different variables you can just use environment variables.
Given I login as "ENV['COUNTRY_NAME']" user using id "ENV['LOGIN_NAME']" and pwd "PASSWORD"
And then when you run the tests
LOGIN_NAME='login name' COUNTRY_NAME=country bundle exec cucumber
And then of course you can put all of the lines you want to run into a bat or sh script.
One thing to be careful of is to use the environment variables or another one to change the path for the outputs so you don't overwrite them.
However, a more elegant solution would be to handle it with a rake task that ran all of the other tasks. The most efficient way to write that would depend on how many different runs you need.
task :all => [:task1, :task2, :task3]
EDIT: To make your scenarios more readable, you should use a generic placeholder in the scenario and hide the environment variables in the step definition.
Given I login as a user
Might have a step definition that looked like:
Given /^I login as a user$/ do
... set up your page object here ...
login_page.login(ENV['COUNTRY_NAME'], ENV['LOGIN_NAME'])
end

Run script in Rails console and have access to objects created?

I recently found you can run an arbitrary Ruby file in the Rails console using load or require, as in:
load 'test_code.rb'
This is great as far as it goes, but using either load or require (what's the difference?) I don't seem to have access to the objects created in the script after it completes.
For example, in my script I might have something like:
u = User.where('last_name = ?', 'Spock').first
If I start rails console and run that script using load or require, I see it working, I see the query happen, and I can 'put' attributes from the object inside the script and see them in the console output. But once the script is done, the variable u is undefined.
I'd like to run some code to set up a few objects and then explore them interactively. Can this be done? Am I doing something wrong or missing something obvious?
Variables defined in your script will go out of scope once the file is loaded. If you want to use the variables in console, define them as instance variables or constants
#u = User.where('last_name = ?', 'Spock').first
or
USER = User.where('last_name = ?', 'Spock').first
As explained in http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-load:
In no circumstance will any local variables in the loaded file be propagated to the loading environment.
An option is to eval the file:
eval(File.read 'your_script.rb')
and the local variables will be there afterwards.

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.

Undefined Method errors on basic AR and AS methods when running threaded with Sidekiq on Heroku

I am getting a couple different errors at a particular line of code in one of my models when running in Sidekiq-queued jobs. The code in question is:
#lookup_fields[:asin] ||= self.book_lookups.find_by_name("asin").try(:value)
I either get undefined method 'scope' for #<ActiveRecord::Associations::AssociationScope:0x00000005f20cb0> or undefined method 'aliased_table_for' for #<ActiveRecord::Associations::AliasTracker:0x00000005bc3f90>.
At another line of code in another Sidekiq job, I get the error undefined method 'decrypt_and_verify' for #<ActiveSupport::MessageEncryptor:0x00000007143208>.
All of these errors make no sense, as they are standard methods of the Rails runtime support libraries.
The model in question has a :has_many association defined for the "book_lookups" model, "name" and "value" are fields in the "book_lookups" model. This always happens on the first 1-3 records processed. If I run the same code outside of a Sidekiq job, these errors do not occur.
I cannot reproduce the error on my development machine, only on production which is hosted at Heroku.
I may have "solved" the first set of errors by putting the code `BookLookup.new()' in an initializer, forcing the model to load before Sidekiq creates any threads. Only one night's work to go on, so we'll have to see if the trend continues...
Even if this solves the immediate problem, I don't think it solves the real underlying issue, which is classes not getting fully loaded before being used. Is class-loading an atomic operation? Is it possible for one thread to start loading a class and another to start using the class before it is fully loaded?
I believe that I have discovered the answer: config.threadsafe!, which I had not done. I have now done that and most if not all of the errors have disappeared. References: http://guides.rubyonrails.org/configuring.html, http://m.onkey.org/thread-safety-for-your-rails (especially the section "Ruby's require is not atomic").

How can I pass a variable into my custom database.yml on heroku for Rails app?

I need to pass variables that I have defined in my custom database.yml. I am loading a database.yml to be used instead of the one that heroku overwrites with.
This variable is determined and assigned in my envirionments/production.rb file.
But I can't seem to get it to assign correctly. It works if I hand-code it, but I cannot do that.
Help?
Try following syntax:
---
variable: &myvar test
something: *myvar
Unfortunately, you cannot join normal string with a variable.
It works to put in tags from an environment

Resources