has_to and belongs_to realtionships are not showing up in database

Hello,

I am having what I might think is a basic problem...I can't get the relationships of has_many and belongs_to mapped out correctly in the database. I would expect, according to my MODEL and MIGRATION, that there would be a desktop_id in the states and environments DATABASE table ?

How can I get the desktop_id, state_id and environment_id columns mapped out in the tables ?

There need to be statements in your migrations to add those columns - rails won't look at the models and work it out from the associations in there.

You may be confusing things with datamapper (that does do this I think) or with the fact that if you do rails g model foo belongs_to:bar then rails will add the belongs_to to the model and the migration.

Fred.

Hello Frederick,

As you suggested, i attempted to include this in my scaffold, but when I rake db:migrate, I get the error:

user@user-user:~/dev/myapp$ rails generate scaffold engine belongs_to:environment belongs_to:state invoke active_record create db/migrate/20121226181404_create_engines.rb create app/models/engine.rb invoke test_unit create test/unit/engine_test.rb create test/fixtures/engines.yml invoke resource_route route resources :engines invoke scaffold_controller create app/controllers/engines_controller.rb invoke erb create app/views/engines create app/views/engines/index.html.erb create app/views/engines/edit.html.erb create app/views/engines/show.html.erb create app/views/engines/new.html.erb create app/views/engines/_form.html.erb invoke test_unit create test/functional/engines_controller_test.rb invoke helper create app/helpers/engines_helper.rb invoke test_unit create test/unit/helpers/engines_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/engines.js.coffee invoke scss create app/assets/stylesheets/engines.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scss user@user-user:~/dev/myapp$ rails generate scaffold environment description:string has_many:engines invoke active_record create db/migrate/20121226181441_create_environments.rb create app/models/environment.rb invoke test_unit create test/unit/environment_test.rb create test/fixtures/environments.yml invoke resource_route route resources :environments invoke scaffold_controller create app/controllers/environments_controller.rb invoke erb create app/views/environments create app/views/environments/index.html.erb create app/views/environments/edit.html.erb create app/views/environments/show.html.erb create app/views/environments/new.html.erb create app/views/environments/_form.html.erb invoke test_unit create test/functional/environments_controller_test.rb invoke helper create app/helpers/environments_helper.rb invoke test_unit create test/unit/helpers/environments_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/environments.js.coffee invoke scss create app/assets/stylesheets/environments.css.scss invoke scss identical app/assets/stylesheets/scaffolds.css.scss user@user-user:~/dev/myapp$ rails generate scaffold state description:string has_many:engines invoke active_record create db/migrate/20121226181504_create_states.rb create app/models/state.rb invoke test_unit create test/unit/state_test.rb create test/fixtures/states.yml invoke resource_route route resources :states invoke scaffold_controller create app/controllers/states_controller.rb invoke erb create app/views/states create app/views/states/index.html.erb create app/views/states/edit.html.erb create app/views/states/show.html.erb create app/views/states/new.html.erb create app/views/states/_form.html.erb invoke test_unit create test/functional/states_controller_test.rb invoke helper create app/helpers/states_helper.rb invoke test_unit create test/unit/helpers/states_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/states.js.coffee invoke scss create app/assets/stylesheets/states.css.scss invoke scss identical app/assets/stylesheets/scaffolds.css.scss user@user-user:~/dev/myapp$ rake db:create user@user-user:~/dev/myapp$ rake db:migrate == CreateEngines: migrating ================================================== – create_table(:engines) rake aborted! An error has occurred, this and all later migrations canceled:

undefined method environment' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000001ca2ff0> /home/user/temp/project/db/migrate/20121226181404_create_engines.rb:4:in block in change’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/schema_statements.rb:160:in create_table' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:466:in block in method_missing’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:438:in block in say_with_time' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:438:in say_with_time’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:458:in method_missing' /home/user/temp/project/db/migrate/20121226181404_create_engines.rb:3:in change’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:407:in block (2 levels) in migrate' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:407:in block in migrate’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in with_connection' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:389:in migrate’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:528:in migrate' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:720:in block (2 levels) in migrate’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:775:in call' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:775:in block in ddl_transaction’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in transaction' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/transactions.rb:208:in transaction’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:775:in ddl_transaction' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:719:in block in migrate’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:700:in each' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:700:in migrate’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:570:in up' /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/migration.rb:551:in migrate’ /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_record/railties/databases.rake:179:in block (2 levels) in <top (required)>' /home/user/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in eval’ /home/user/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `’ Tasks: TOP => db:migrate (See full trace by running task with --trace)

Hello Frederick,

As you suggested, i attempted to include this in my scaffold, but when I rake db:migrate, I get the error:

You probably don't want to include the has_many - since that requires a column on the other table. Other than that, take a look at the generated migration.

To be honest i don't bother with the belongs_to shortcuts for migrations - I just add the columns as 'normal' columns and then edit the model

Fred