I am trying creating user-specific posts in the seeds file. the directions I am following tell me to reset the database. In the rails c (console) I entered:
rake db:reset
It gave me this error. Doesn't make sense.
NameError: undefined local variable or method `reset' for main:Object
rake command is to be run from the command line; not from within the rails console.
See Rails gives NameError for all command line methods such as generate or rake for the same mistake.
In previous apps when the same database data has been required by everyone we've used seed_dumper & rake db:seed:dump to populate the seeds file and then rake db:seed.
For this app, using the Comfortable Mexican Sofa CMS gem and the fortress plugin it would be nice to do the same. Only seed_dumper only dumps the users - nothing else.
seed_dump (the 'parent' gem) gives the following error:
rake aborted!
NameError: undefined local variable or method `cms_manageable' for Comfy::Cms::Page(Table doesn't exist):Class
/usr/local/rvm/gems/ruby-2.0.0-p247#global/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
/path to my app/app/models/comfy/cms/page.rb:9:in `<class:Page>'
Any ideas or alternate methods would be appreciated. Cheers.
Now using only gem 'seed_dump' (not seed_dumper), running the command bundle exec rake db:seed:dump works for exporting seed data.
I am creating a brand new full gem plugin using Rails 3.1.0 on ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]. I am having trouble with the schema and fixtures not being loaded for running rake test. Following are the steps I am taking:
Create the plugin:
rails plugin new core --full
From the plugin, generate a new scaffold:
rails g scaffold user
Run the db create and migrate:
rake db:create
rake db:migrate
Run the tests:
rake test
When running the functional tests, I am receiving a set of error like the following from the controller tests:
1) Error:
test_should_create_user(UsersControllerTest):
NoMethodError: undefined method `users' for #<UsersControllerTest:0x00000003babca8>
This error seems to stem from the fact that the test doesn't seem to understand the fixture call. It doesn't look like the test db is getting the schema loaded or having the fixtures loaded. Is this expected behavior? Is there something I'm missing in this scenario? All the fixtures are there. Is there some process I need to follow to get these tests to run correctly?
OK, I think I've made some progress. It looks like I can resolve it with the following changes.
I've added the following to test_helper:
#Run any available migration
ActiveRecord::Migrator.migrate 'up'
# Set fixtures root
ActiveSupport::TestCase.fixture_path=(File.expand_path("../fixtures", __FILE__))
ActiveSupport::TestCase.fixtures :all
This allows the test environment to find my fixtures outside of the dummy application.
After this change, I was still having an issue where the test database was not being prepared for the test. I resolved this by dropping into the test/dummy directory and running:
rake db:test:prepare
After this, I am able to run "rake test" successfully. It's somewhat a pain to have to drop in to the dummy app to prepare the db. There has to be a way around this. Any suggestions?
I am trying to install the spree gem using the following instructions http://spreecommerce.com/resources/quick-start
I am getting following error on step 4.6(Populating the Database)
$ rake db:bootstrap
.....
.......
rake aborted!
couldn't parse YAML at line 6 column 50
....
.....
I am using ruby 1.9.2p0, rails 3.0.3 and spree 0.40.2.
Can anyone please tell me what could be the problem?
There's probably an error in a YAML file, not surprisingly. It's likely a fixture used in the bootstrapping process.
You can always run rake with the --trace option to get a better sense of what it was trying to do. Have a look through your fixture files to see if any are invalid, especially at the position indicated.
In boot.rb file inside config directory of my rails project home I added following and it worked.
require 'yaml'
YAML::ENGINE.yamler= 'syck'
I followed instruction from http://www.ruby-forum.com/topic/1002689
The background: I'm having some problems with Thoughtbot's "Factory Girl" gem, with is used to create objects to use in unit and other tests. I'd like to go to the console and run different Factory Girl calls to check out what's happening. For example, I'd like to go in there are do...
>> Factory(:user).inspect
I know that you can run the console in different environments...
$ script/console RAILS_ENV=test
But when I do that, Factory class is not available. It looks as though test_helper.rb is not getting loaded.
I tried various require calls including one with the absolute path to test_helper.rb but they fail similarly to this:
$ script/console RAILS_ENV=test
>> require '/Users/ethan/project/contactdb/test/test_helper.rb'
Errno::ENOENT: No such file or directory -
/Users/ethan/project/contactdb/config/environments/RAILS_ENV=test.rb
Grr. Argh.
For Rails < 3.0
Run script/console --help. You'll notice that the syntax is script/console [environment], which in your case is script/console test.
I'm not sure if you have to require the test helper or if the test environment does that for you, but with that command you should at least be able to boot successfully into the test env.
As a sidenote: It is indeed kind of odd that the various binaries in script/ has different ways of setting the rails environment.
For Rails 3 and 4
Run rails c test. Prepend bundle exec if you need this for the current app environment.
For Rails 5 and 6
Run rails console -e test.
In Rails 3, just do rails console test or rails console production or rails console development (which is the default).
For Rails 5.2.0: "Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead."
rails c -e test
script/console test
Should be all you need.
You can specify the environment in which the console command should operate.
rails c [environment]
Examples
1) For Staging
rails c staging
2) For Production
rails c production
For source & detailed description: The Rails Command Line
David Smith is correct, just do
script/console test
The help command will show why this works:
$ script/console -h
Usage: console [environment] [options]
-s, --sandbox Rollback database modifications on exit.
--irb=[irb] Invoke a different irb.
--debugger Enable ruby-debugging for the console.
It's the [environment] bit.
I share the asker's pain. There are really three separate questions here, some of which are addressed, some not:
How do you start the console in the test environment?
For recent Rails versions, bundle exec rails c test, or alternative syntaxes for that.
How do you ensure that test/test_helper.rb is loaded into that console session?
Something like require './test/test_helper' ought to do it.
For me, this returns true, indicating that it was not already loaded when I started the console. If that statement returns false, then you just wasted a few keystrokes, but you're still good to go.
Once test_helper is loaded, how do you call the methods defined in it?
In a typical test_helper, the custom methods are typically defined as instance methods of ActiveSupport::TestCase. So if you want to call one of them, you need an instance of that class. By trial and error, ActiveSupport::TestCase.new has one required parameter, so...pass it something.
If your test_helper has a method called create_user, you could invoke it this way:
ActiveSupport::TestCase.new("no idea what this is for").create_user
Make sure you installed the GEM and you added the following line either in your environment.rb or test.rb file.
config.gem "thoughtbot-factory_girl", :lib => "factory_girl", :source => "http://gems.github.com"
Test Env
rails console test # or just rails c test
Developement Env
rails console # or just rails c
Command to run rails console test environment is
rails c -e test
or
RAILS_ENV=test rails c
if you are facing problem something like
ActiveRecord::StatementInvalid:
Mysql2::Error: Table 'DB_test.users' doesn't exist: SHOW FULL FIELDS FROM `users`
then you should first prepare your test DB by running
bundle exec rake db:test:prepare