I'm creating a plugin for my Rails 2.3.8 and inside my plugin's init.rb file I have the following code
"#{RAILS_ROOT}/log/myerror.log"
I'm trying to create a 'myerror.log' file in log/ folder), but it seems like plugin can't read the RAILS_ROOT variable (I'm getting the following error in my plugins unit tests:
`const_missing': uninitialized constant RAILS_ROOT (NameError)
I did some web-searching but couldn't find an answer. How to run unit tests with in a plugin with 'RAILS_ROOT') variable, or what are the other best practices?
I'm running on Rails 2.3.8 on Linux.
You should define the RAILS_ROOT constant by using
RAILS_ROOT = File.join(File.expand_path(File.dirname(__FILE__)), '../../../')
Related
I have a file lib/stock_reader.rb in which I'm trying to create a model like so:
module StockReader
def self.create_company_reports(company_data)
CompanyReport.create(name: company[:name])
end
end
In another file, lib/curate.rb, I call this method:
require_relative 'stock_reader'
StockReader.create_company_reports(company_data)
But I receive the error:
/Users/me/code/applications/curator/lib/stock_reader.rb:38:in `block in create_company_reports': uninitialized constant StockReader::CompanyReport (NameError)
from /Users/me/code/applications/curator/lib/stock_reader.rb:37:in `each'
from /Users/me/code/applications/curator/lib/stock_reader.rb:37:in `create_company_reports'
from lib/curate.rb:12:in `<main>'
It seems that my lib directory is failing to recognize my model's existence in app/models/company_report.rb:
class CompanyReport < ActiveRecord::Base
end
I'm guessing this may be because the lib/ directory is being loaded before app/models, but I'm not sure.
I've looked at Accessing models from within the lib directory in a Rails 3 project but I can't see where my lib/ directory is being required in any rakefiles.
It happens when I test lib/curate.rb by running $ ruby lib/curate.rb
Well, that would explain it.
Ruby doesn't know anything about CompanyReport. Rails knows where to find your models because it has a ton of code that handles autoloading classes, but Ruby isn't Rails. If you want code to use Rails' features, you need to run the code in the "Rails environment."
There are a few ways to do this. If you want to run an arbitrary script (like lib/curate.rb) in the Rails environment, you can use the rails runner command:
$ bin/rails runner lib/curate.rb
The Rails console is also very useful for testing:
$ bin/rails console
Loading development environment (Rails 4.1.6)
irb(main):001:0> require Rails.root + "lib/curate"
It's pretty rare to use the ruby command in a Rails project, because usually you want to use Rails' features. You'll probably use the above commands a lot more often.
The problem is that CompanyReport class does not exist in the StockReader module where it's looking for it. To use the top level CompanyReport model preface the class name with ::
::CompanyReport.create(name: company[:name])
I'm working on a rails gem, in which I have some logic that I'd like to be conditional based on the rails environment.
The following code errors out:
if Rails.env.production?
When running in the test app this gives me:
undefined method .env for Gemname::Rails::Module
So, how do you find the Rails environment from a method call in a module that's in a gem?
You have a Rails module in your project, and the constant lookup is finding it, rather than the top-level Rails module. You can either use the top-level constant:
::Rails.env.production?
Or you can just check the environment variable:
ENV['RAILS_ENV']
I have to run this script: /scripts/saveData.rb
And in it, I need to use ActiveRecord of my Rails application.
I tried to invoke Class, but I get error "uninitialized constant (NameError)".
How can I get a reference to the Rails classes from my saveData.rb script?
Rails 3.1
UPDATED: how i can invoke a method of a Model? My model is: "Program" (also Active Record). I tried with "Program.method" but it doesnt works, why?
Which version of rails you are using?
You can run your code in Rails runner like this if you are using Rails 3:
http://guides.rubyonrails.org/command_line.html#rails-runner
$ rails runner script/saveData.rb
If you are using Rails 2, try this one:
$ script/runner script/saveData.rb
Use rails runner.
Barring that, you could create a rake task, or just load the file from the rails console.
You could try to require the Rails environment with this at the top of your script:
require File.expand_path(File.join(File.dirname(__FILE__), 'config', 'environment'))
However, you should really consider using a rake task instead.
I'm trying to add an entire folder to the JRuby 1.5 classpath for my Rails app. The JRuby Wiki suggests the following: "... add the config directory to the JRuby classpath in config/environment.rb:"
$CLASSPATH << "file:///#{File.expand_path(File.join(RAILS_ROOT, 'config'))}/"
That doesn't seem to work for me. It doesn't matter whether I put that before, after or inside of the Rails::Initializer.run block. No matter what, I get:
/home/sean/src/sbruby/seo/config/environment.rb:45:NoMethodError: undefined method `<<' for nil:NilClass
/home/sean/apps/jruby/jruby-1.5.0/lib/ruby/gems/1.8/gems/rails-2.3.7/lib/rails/backtrace_cleaner.rb:2:NameError: uninitialized constant ActiveSupport::BacktraceCleaner
/home/sean/apps/jruby/jruby-1.5.0/lib/ruby/gems/1.8/gems/rails-2.3.7/lib/console_with_helpers.rb:5:NameError: uninitialized constant ApplicationController
For example, I'm trying to add a folder under RAILS_ROOT called resources/foobar, so I added the following to environment.rb:
$CLASSPATH << "file:///#{File.expand_path(File.join(RAILS_ROOT, "resources", "foobar"))}/"
Same error.
What is the right way to add a folder to the JRuby classpath with Rails?
Require java first. That's what makes the $CLASSPATH variable live.
include Java
$CLASSPATH << "your/folder"
In pre-1.0 versions of JRuby, you'd do require 'java' instead, but in modern JRuby that silently doesn't work.
I installed the Mocha 0.9.7 Rails plug-in using:
$ script/plugin install git://github.com/floehopper/mocha.git
(Just followed instruction in http://mocha.rubyforge.org/)
Then, I have the following set-up defined in my functional test
def setup
#controller.expects(:logged_in?).returns(true)
#controller.expects(:admin_user?).returns(true)
end
Running the test generates the ff. error:
NameError: uninitialized constant Mocha::Mockery::ImpersonatingName
/test/functional/xxxx_controller_test.rb:x:in `setup'
Before that, I see the ff. error at the top of the test log:
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/test_case.rb:12: warning: already initialized constant Mocha
This has led me to believe that I have an old version of Mocha somewhere in Ruby's or Rails' path. The problem is I can't find it.
Is my guess correct? If so, where is this old version of Mocha? Alternatively, how can I found out where it is?
According to http://selfamusementpark.com/blog/2009/07/30/rails233mochaconfusion/, my guess is not correct. The problem really is that Mocha is being loaded before the testing framework which is not what the former expects. The solution is to edit RAILS_ROOT/vendor/plugin/mocha/init.rb to comment out the ff. line:
require 'mocha'
Then, Mocha will have to be explicitly required in the test files or helpers to ensure that the testing framework will have been loaded beforehand.