set rails console stack backtrace limit permanently - ruby-on-rails

rails console by default boots with context.back_trace_limit=16, which can be changed to whatever you want simply by typing context.back_trace_limit=n. The problem is you have to type it each time you boot rails c. Where do I change the context.back_trace_limit permanently? Some more reading on rails console configuration appreciated.

You have to create/edit your ~/.irbrc with the following:
IRB.conf[:BACK_TRACE_LIMIT]= 20
To be taken into account:
The options must be uppercased
This option is changing not only the
rails console, but the normal "irb" behavior (the rails console uses
irb to run)
This setting is global, and not per project
Reference http://rakeroutes.com/blog/customize-your-irb/
Update for Rails 5
In Rails 5 the command context.back_trace_limit=20 will fail.
In the console you need to use the command conf.back_trace_limit = 10 for the current session.
For permanent changes, writing IRB.conf[:BACK_TRACE_LIMIT]= 20 in your ~/.irbrc is still valid.
You can see the current settings by calling conf

Related

Define "current time" when starting Rails?

I have an app that changes behavior based on the time of the day, and I'd like to be able to just spin up the server and test how it works at different times.
Ideally, I'd like to be able to just pass in the time as an argument when I run rails s, like rails s TIME=2014-02-26 22:06:11 -0500.
In config/locales/application.rb you can set the timezone before running rails s.
config.time_zone = 'Alaska'
If you're running OS X, you can also set the system time in the terminal before running rails s.
date 022611002013
The format is [month][day][hh][mm][year], so the above is 11:00 February 26, 2013. You can change your system time back to the current time in the terminal or Date and Time preferences panel.
You can actually read environment variables from within rails (actually Ruby). This is how to do it:
require 'date'
$startup_time = DateTime.parse(ENV['STARTUP_TIME']) rescue DateTime.now
puts "Server started at: #{$startup_time}"
You'd call this code placed in pull_time.rb like this:
STARTUP_TIME="2014-02-26 22:06:11 -0500" ruby pull_time.rb
In order to have this global variable work in Rails, you want to add it to an initializer.
So put the code above in config/initializers/set_startup_time.rb
You can now access $startup_time anywhere in your code. Just pass STARTUP_TIME as an environment variable to rails.
STARTUP_TIME=2014-02-26 22:06:11 -0500 rails s
Rails server uses time of the hosting machine. So if you are running on local machine then changing the time of the local operating system will also have effect on your rails server
If it's hosted on another server then you have to change that server's time.
Don't try changing the time of your server. That can play real havoc with cron jobs or other time-based services.
Instead, have you tried defining the starting time as a specific value in your code then running the server? Then set it to the next time you need, and run the server again?
Time.new(2013, 12, 31, 12, 59, 59) # => 2013-12-31 12:59:59 -0700
You could also use the time as an integer, using the number of seconds that represent the time you want the server to think it is. Pass that in as an ENV hash element, then check to see if it's defined. If it is, define the time based on that value, otherwise don't define the ENV value and the server will run as normal:
ENV['SERVER_TIME'] = '1388519999'
server_time = ENV['SERVER_TIME'] ? Time.at(ENV['SERVER_TIME'].to_i) : Time.now
server_time # => 2013-12-31 12:59:59 -0700
Then, when you need to test the server, you could write a little shell script that sets that value and runs rails s, test the server then quit it, and rerun the shell script with a different value.
You can change Rails' internal timezone by adding config.time_zone = 'Pacific' to application.rb.
If you're deploying to Heroku, you can do the same slightly differently: heroku config:add TZ="Europe/Athens".
However, the downside is that neither of these commands allows you to define a specific time for the app (just the timezone, so you get the current time in another timezone), nor can you run these from command-line when starting your local Rails server.
Rails server determine time from the hosting machine
So changing the system's time would do the trick
EDIT:
As 'sevenseacat' said in comment
you can also use gem timecop to test your application without changing system's time.

How to specify environment dependencies in Rails?

Where does one list the environment variable dependencies for a Rails application?
I don't want the app to run if the user hasn't specified the variables or at a minimum output some form of notice that says ***Don't run until you've set the following environment variables..."
I'd put something like that in config/boot.rb:
# usual boot.rb stuff...
raise 'Set PANCAKES in your environment!' unless ENV.has_key? 'PANCAKES'
The nice thing about boot.rb is that it is run very early in the start up process so you don't have to wait for all the Rails machinery to start (which can take a long time) before you know there's a problem.

How to invoke rails migration generator programmatically?

Simply Running rails g from command line suffers from long time of pre-loading. I want to fast generate multiple migrations from a running rails c, so I tried:
require 'rails/generators'
Rails::Generators.invoke('migration',['user_book_list_followings'],{:behavior=>:invoke, :destination_root=>Rails.root})
You ask where did I get that :behavior and :destination_root arguments? Well, I pried at the Rails::Generators#invoke execution point when I run the generator from the command and recorded the exact arguments.
However, the funny thing was, I was presented with a error saying:
No value provided for required options '--orm'
Did I miss anything? (I was on Rails 3.2.3)
require 'rails/generators'
Rails::Generators.invoke("active_record:migration", ["create_something", "list_order:string", "name:string"], {migration: true, timestamps: true})
One solution to speed up callings rails g, is using spring.
Spring is a Rails application preloader. It speeds up development by keeping your application running in the background so you don't need to boot it every time you run a test, rake task or migration.

ActiveRecord: Name already in use within the application, where?

I'm attempting to generate a scaffold but generating it I receive the following error:
rails generate scaffold foo
invoke active_record
The name 'Foo' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
Is there a command to find out exactly where this name is being used within my application?
I don't think there is a way to find the file or source of any object/class/module. Also rails has open class concept so the class can be defined or refined in many files so we can not track the same.
but you can check if the name is exist for any object by following
Module.constants.include? "Foo"
Using rubymine, there are a few ways you can do this. There is a "find usages" command that will find all location where a method, variable, etc. are used. There is a "jump to declaration" which in your case would be useful. It will jump to the spot where something is defined. (a class, module, variable, method). there is also a powerful search feature. In this case, search in path would allow you to search the entire application (including external gems being used). You can force case sensitivity on your search to only yield class / module names etc.
Szuper tricky! For me the model name was stuck in memory in the Spring cache system. Had to kill the spring process to free it up.
Look for this when you attempt the command:
Running via Spring preloader in process 57104
Expected string default value for '--serializer'; got true (boolean)
invoke active_record
The name 'Admin' is either already used in your application or reserved
by Ruby on Rails. Please choose an alternative and run this generator
again.
If you see that Spring comment, try looking for spring in your processes and killing:
ps -ef | grep spring
find the id:
501 54701 30654 0 1:43PM ?? 0:04.83 spring app | server | started 8 mins ago | development mode
501 30654 1 0 Tue03PM ttys000 0:03.82 spring server | server | started 142 hours ago
and kill
kill 30654
Thats just one of the given possibilities! Foo is a reserved Word. Ruby also reserves words that arent used as a Model/Module name already. For example you also cant create a model called Configuration eaven there is no class thats clled Configuration.

Debugging/Breakpoint-ing the Rails Core with Ruby-Debug?

How do I debug the rails gems? I've just found the beauty of ruby-debug and am real excited, but I want to step through the routing code in action_controller, but the debugger isn't set up at this time, it seems like it's just skipping over my "debugger" calls in action_controller/routing/route_set.rb for example.
What am I missing?
I just tested this with Rails 2.3.4. I added a 'debugger' line to the call method in vendor/rails/actionpack/lib/action_controller/routing/route_set.rb, ran 'rdebug script/server', browsed to a page, and it stopped at the correct line.
You can also use a class/method breakpoint; you'll need to step through the first few lines of the app until you're past the require 'action_controller' line, and then enter: b ActionController::Routing::RouteSet.call.
Or you can try setting a breakpoint based on the file name and line number.

Resources