Rails 3 console always starts in test environment - ruby-on-rails-3

My rails 3 app always is stuck in the Test environment. When I call
rake db:reset
it resets the test database, but not the development one.
When I run the following code, it loads the test environment in the console:
rails c
Trying to specify the development environment also does not work:
jon#jon-laptop:~/id$ RAILS_ENV=development rails console
Loading test environment (Rails 3.0.8)
ruby-1.8.7-p334 :001 >
Starting the server does work normally:
rails s
This is very annoying. Any ideas on where I should look to resolve this?
Thanks in advance,
EDIT
I also tried going back in history to earlier commit to before the problem existed (I think) and it does not fix the problem...

The easiest way is to set the environment is probably by using the RAILS_ENV environment variable, e.g.:
RAILS_ENV=test rails console
Edit: Which version of Rails are you using? This works fine for me on 3.0.7:
rails c [environment]
E.g.
rails c development

I found the problem. I had the following lines in one of my initializers:
ActionMailer::Base.default_url_options[:host] = "localhost:3000" if Rails.env == "development"
ActionMailer::Base.default_url_options[:host] = "localhost:3000" if Rails.env = "test"
Can you spot the mistake???

Related

Rails friendly_id: undefined method `slug` on production

I'm trying to introduce dynamic_sitemaps over resources with friendly_id. The issue is the production rails (rake / rails c) doesn't see the slug method. I've try to specify it by force by specifying an attr_accessible :slug, but it doesn't help either.
$ rake sitemap:generate
Generating sitemap...
rake aborted!
undefined method `slug' for #<Article:0xa9e4d14>
The funny thing it works smoothly on the local environment, and it should not be so much different with the capistrano/rvm deployment.
The column exists in the DB and is used by the rails app itself (which works fine too).
Added: it should be tied to either the environment or the specific gem version issue, but I'm not sure which one is the trouble, and how to debug it. The same pair works good for a different project with a pretty similar libraries bundle.
As the capistrano always do the dirty work, I forgot about the RAILS_ENV environment variable - so the console and cron job tried to operate against the dev DB and obviously failed.

Unable to access config.handlebars in production mode

I've got an existing rails app, and I've added an ember front-end. I'm having trouble deploying the new version (which includes Ember for the first time) to Heroku.
The problem is that I'm unable to run rake tasks in production mode.
I discovered this when I tried to rake db:migrate on heroku. I got the following error:
rake aborted!
undefined method `handlebars' for #<Rails::Application::Configuration:0x00000004f0de90>/app/.bundle/gems/ruby/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configuration.rb:85:in `method_missing'
/app/config/application.rb:60:in `<class:Application>'
I get the same error if I try to run any tasks locally in production mode, e.g.:
RAILS_ENV=production rake -T
the offending line, from config/application.rb:
config.handlebars.templates_root = 'ember/templates'
for various reasons, I had to move the ember templates down one file level. and it needs to stay there. everything works fine in development mode.
Any idea how I can fix this?
tried upgrading the ember-rails gem. this didn't help. (I'm using 0.12.0)
trick was to move ember-rails gem out of assets group

Is it possible to check the rails environment for what rake task is running?

Is there an environment variable I could leverage in my rails initializers to determine if a rake task is executing ? Like -- rake db:migrate db:seed
I have a bunch of initializers that could be skipped for most rake tasks:
Don't load spring (it's a jruby project)
Don't load the audit observer that breaks the migration
Update:
I'm probably going to regret this later -- but the following seems to work --
In my application.rb -- have added the following:
config.is_rake = (File.basename($0) == 'rake')
Then I am checking for the value later on
config.active_record.observers = :audit_observer unless config.is_rake
Elsewhere in my spring initializer
SPRING_CONTEXT = org.springframework.context.support.FileSystemXmlApplicationContext.new(SPRING_XML_CONFIG_FILES) unless Rails.application.config.is_rake
Based on answer found here

Error while running Metrical

I've a Ruby 1.9.2 and Rails 3.0.9 development environment. When I run the gem "metrical" on my application, I get the following errors:
** Running the specs/tests in the [test] environment
Analyzing: 100% |oooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:00
/Users/tester/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/visitors/emitter.rb:17:in `end_document': undefined method `write' for #<Syck::Emitter:0x000001028d2388> (NoMethodError)
Anyone else seen this issue? I'm out of ideas for the error.
I ran into the same problem, seems to be related to YAML parsing with Ruby 1.9.2
I hacked into metric fu and inserted a explicit requirement of the Psych YAML Engine:
metric_fu-2.1.1/lib/metric_fu.rb:
require 'rake'
require 'psych' # <-- added here
require 'yaml'
And now works...
The simplest solution is to set the RUBYOPT environment variable when you run metrical, so instead of metrical you type:
RUBYOPT='-rpsych' metrical
I am doing that and it's all working fine. This blog post figured it out for me: http://excid3.com/blog/undefined-method-write-for-syckemitter/ (possibly a bit simpler than hacking into metric_fu, until they fix this).

Get Cucumber to use the test environment in Sinatra

This seems right, but doesn't seem to work.
env.rb:
class MyWorld
set :environment, :test
end
app.rb:
configure :development do
DataMapper::setup(:default, "sqlite3://development.sqlite3")
end
configure :test do
DataMapper::setup(:default, "sqlite3://test.sqlite3")
end
It keeps using the development environment. Am I missing something, or am I doing it wrong?
Put this at the top of env.rb, and things work perfectly:
env.rb
ENV['RACK_ENV'] = 'test'
Alternatively, this will do the same without having to edit any files:
$ RACK_ENV=test cucumber features
You might want to look into the cucumber-sinatra gem. It has options to autogenerate a minimal amount of code (including your Sinatra app & rackup file). It should provide the correct syntax for getting cucumber scripts to run in test configuration.