I'm working through the Ruby on Rails Bible using Windows 7 and Rails 3 + mysql.
I created a database and a table in mysql directly as per instructions.
Then I created a model called Contact
Next in irb I entered:
my_contact=Contact.new and then I get the error:
"unitialized constant Object::Contact"
I think perhaps I have to precede the code with a require statement or perhaps I need to install a gem? Except I haven't a clue beyond that at this stage as I'm a newbie...
Instead of running irb manually, run rails console, this should load all dependencies for your app. Also make sure you have run rake db:migrate before starting the console.
Related
I created a new rails 4 engine and added a model. I am trying to migrate the database using
RAILS_ENV=test rake db:migrate
and it comes back with no errors. However, when I run:
rspec spec
an error returns stating there are pending migrations.
Migrations are pending; run 'rake db:migrate RAILS_ENV=test' to resolve this issue. (ActiveRecord::PendingMigrationError)
It's true if I look at my database (tried on pg and sqlite) that they have not been run and no tables have been created. Running the suggested command listed above does not run the migrations.
There is only one migration in 'engine_name/db/migrate', and no migrations inside the dummy app.
I am using ruby 2.0 and rails 4.0.0.rc1.
You need to copy migrations into dummy app rake app:<engine_name>:install:migrations
Add code in your engine task
https://gist.github.com/doabit/5692865 .
I've experienced this same issue. doabit's fix worked for me. thanks!
There is an open issue for this with the rails core team.
https://github.com/rails/rails/issues/10622
I just joined a project developing a rails engine, that also has a dummy app for testing.
foo/
foo/spec/dummy/
There are identical migrations in
foo/db/migrate/
foo/spec/dummy/db/migrate/
If I rake db:migrate from the dummy app, all is well. If I do the same from the engine (current directory = foo) I get an error about multiple migrations with the same name.
Q1) Are the Rakefiles borked? (should db:migrate recurse down to the dummy app?)
Q2) Should the migrations only be in one directory? If so, which one?
We are using Rails 3.2.9, ruby 1.9.3p194.
Question 1
The Rakefile should have an entry to account for the spec/dummy app. For example,
Bundler::GemHelper.install_tasks
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'
Here's more detailed example rakefile, https://github.com/twinge/questionnaire_engine/blob/engine2/Rakefile
Question 2
IMO, the migrations should only exist on the foo/db/migrate folder, and not the foo/spec/dummy/db/migrate. In fact, I don't version control the dummy's db/migrate or the db/schema.
Why? I use the dummy app the make sure a full on install of my engine works 100%. Therefore, if I version controlled the foo/spec/dummy db state, I would be testing as if there was a previous install.
Example Engine
https://github.com/twinge/questionnaire_engine/tree/engine2
For testing the dummy app, you can run your engine migrations for the test ENV using the following:
RAILS_ENV=test rake db:migrate
I currently run my Rails app using:
jruby --1.9 -J-XX:+CMSClassUnloadingEnabled -J-XX:+UseConcMarkSweepGC -J-XX:MaxPermSize=256m -S rails server
This is getting pretty old now. How can I set my Rails project up so that just running
rails server
has the same effect?
(Note: bash aliases and the like are not what I'm looking for here. I want to make the project work right, not fix my local settings)
When using RVM and a project .rvmrc, the canonical way is to set PROJECT_JRUBY_OPTS in the project .rvmrc. A bug prevented this from working for me, so use rvm head.
If not using rvm then use JRUBY_OPTS, which is the built-in way of doing it that JRuby checks (in fact, the PROJECT_JRUBY_OPTS thing ends up being converted to JRUBY_OPTS by rvm).
I am novice rails/terminal user and just did a clean install of Lion + Xcode + Rails. Unlike before (on Snow Leopard), I now get an error running rake db:migrate.
I have cloned my code through git which worked fine and created the database witht the "createdb" command but when I try run "rake db:migrate" in terminal it now comes up with this error:
rake aborted!
development database is not configured
My config/database.yml file looks like below in the development section which is exactly the way it looked before on Snow Leopard where it worked fine, so don't know if the error I am now getting is related to Lion.
development:
adapter: postgresql
database: my_db
username: rasmus
encoding: utf8
pool: 5
Can anyone help, please?
I got the same error and in my case it was because the database.yml was not indented correctly. All the configuration parameters should be indented.
Note, be sure to follow the proper spacing conventions. The database config is whitespace aware. Two spaces per attribute works fine. In the following code, note how each attribute has two spaces. Do not use tabs. If you don't use spaces for attributes, rake will not work and throw the same error.
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: unicode
database: db/production
pool: 5
timeout: 5000
password:
You might also want to look for syntax errors in the file. This is the error that will appear if you have a syntax error in your config/database.yml file and you try to do something like start the local web server or run rake db:migrate.
In my case I had accidentally removed the comment from a line at the top of the file and I was seeing this error since the uncommented line made this an invalid yml file.
Solved!
My "gem install pg" had not been run so basically I was missing the pg gem. After "gem install pg" in terminal everything works fine.
Here's a PEBCAK answer for Googlers - check your Gemfile and make sure you have specified your database adapter gem in the proper group in your Gemfile. I had mine defined for only :production and :staging, and at one point must have manually ran gem install pg on my development machine after switching from mysql. This morning I emptied all of the gems for the app and re-bundle install-ed them, then couldn't figure out why the database wouldn't connect. Moving the pg gem spec outside of any group and running bundle install resolved the problem.
A note to others who land on this question page: be sure that you are running the rake db command correctly, i.e:
rake db:migrate instead of rake db migrate
What worked in my case, having tried all the above when rake db:create failed, was to make sure that my Rakefile was properly configured.
This did the job:
require "sinatra/activerecord/rake"
require 'sinatra/asset_pipeline/task'
namespace :db do
task :load_config do
require "./app"
end
end
The rails command-line command provides a couple of commands, like rails generate, rails console etc. Now I'd like write a gem which registers my own command for use with rails mycommand.
Is this possible?
If so, any guides on how to do that?
NB: This is for rails 3+
regards, apeiros
Haven't done it, but here are some leads. In your Rails app, the script directory holds a file called 'rails' that has this line
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
It then requires this Rails file: https://github.com/rails/rails/blob/master/railties/lib/rails/commands.rb