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']
Related
I'm wondering if it's possible to start a Byebug session giving a starting point from a Rails console. I know I can insert a byebug statement wherever I want and start debugging, but I'd like to do something like this:
Byebug.start do
# entry point
User.find(12).problematic_method
end
Thanks.
I opened the class and override the problematic_method inside the Rails console and added the byebug statement where I wanted it. This way I don't have to change the running production code (I forgot to mention above I want to debug in production).
This workaround will be enough for my purposes. The only problem is that you don't have the debug code listing available for that method, but its fine.
That is not possible. What you can do, is write your code inside a .rb file and debug that file/script using byebug.
I'm using system(" ") on the rails console to execute an application that takes inside a file, processes it and outputs another file.
The application also logs statistics for said file on the terminal before closing.
My objective is to save those logged statistics as variables inside rails, although I'm not exactly sure how I can do that.
How can I save the logged statistics inside rails variables?
I don't think the system method is the best method to use here, as I think this would either return true or false. However to get back the value, you should try to use the backticks
statistics = `method to call on computer`
let me know if that works
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.
I have a Rails application, and I'm trying to export data, but directly through Pry because I only need to do it once.
Is this possible with Pry? I looked at the documentation but doesn't seem like there's an easy way to dump console data anywhere.
I have a hash, with nested hashes/objects, which I need to send over to a 3rd party for work with an API. They need a dump of the data so they can set up the receiving end of my call. I'm just going to do this in Ruby now, but it would have made more sense to dump the data through PRY, rather than edit my ruby object to dump the data, which I only need once.
If you can start the server from a local command-line, or SSH to the host and run an instance there, you can use Pry for this. Basically you need to add these lines to your code at the appropriate place:
require 'pry-debugger'; binding.pry
which will stop your code and put you at the Pry prompt. At that point you can enter something like:
require 'json'
File.write('test.data', hash.to_json)
Read the Pry.debugger documentation for information about using Pry with remote Rails sessions, which might work better for you.
You can also export any string into a file (here output.txt):
x = 'something funky'
.echo '#{x}' > output.txt
Just be careful with quotes in the string. These may lead to problems in the shell.
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