Julien Estebane
2012-Dec-26 02:25 UTC
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 ? I ran rake db:migrate before going to the rails console and checking the tables... MODEL: ------ user@user-DevMachine:~/dev/myapp/app/models$ cat engine.rb class Engine < ActiveRecord::Base # attr_accessible :title, :body belongs_to :environment belongs_to :state end user@user-DevMachine:~/dev/myapp/app/models$ cat environment.rb class Environment < ActiveRecord::Base attr_accessible :description has_many :engines end user@user-DevMachine:~/dev/myapp/app/models$ cat state.rb class State < ActiveRecord::Base attr_accessible :description has_many :engines end MIGRATION: ---------- user@user-DevMachine:~/dev/myapp/db/migrate$ cat 20121225230016_create_engines.rb class CreateEngines < ActiveRecord::Migration def change create_table :engines do |t| t.timestamps end end end user@user-DevMachine:~/dev/myapp/db/migrate$ cat 20121225230034_create_states.rb class CreateStates < ActiveRecord::Migration def change create_table :states do |t| t.string :description t.timestamps end end end user@user-DevMachine:~/dev/myapp/db/migrate$ cat 20121225230118_create_environments.rb class CreateEnvironments < ActiveRecord::Migration def change create_table :environments do |t| t.string :description t.timestamps end end end user@user-DevMachine:~/dev/myapp/db/migrate$ DATABASE: --------- user@user-DevMachine:~/dev/myapp/db/migrate$ rails console Loading development environment (Rails 3.2.9) 1.9.3-p327 :001 > ActiveRecord::Base.connection.execute("SELECT * FROM engines") (1.1ms) SELECT * FROM engines => [{"id"=>1, "created_at"=>"2012-12-26 01:26:38.789476", "updated_at"=>"2012-12-26 01:26:38.789476", 0=>1, 1=>"2012-12-26 01:26:38.789476", 2=>"2012-12-26 01:26:38.789476"}] 1.9.3-p327 :002 > ActiveRecord::Base.connection.execute("SELECT * FROM environments") (0.3ms) SELECT * FROM environments => [{"id"=>1, "description"=>"warm", "created_at"=>"2012-12-26 01:27:40.968920", "updated_at"=>"2012-12-26 01:27:40.968920", 0=>1, 1=>"warm", 2=>"2012-12-26 01:27:40.968920", 3=>"2012-12-26 01:27:40.968920"}, {"id"=>2, "description"=>"cold", "created_at"=>"2012-12-26 01:27:41.055711", "updated_at"=>"2012-12-26 01:27:41.055711", 0=>2, 1=>"cold", 2=>"2012-12-26 01:27:41.055711", 3=>"2012-12-26 01:27:41.055711"}, {"id"=>3, "description"=>"hot", "created_at"=>"2012-12-26 01:27:41.129397", "updated_at"=>"2012-12-26 01:27:41.129397", 0=>3, 1=>"hot", 2=>"2012-12-26 01:27:41.129397", 3=>"2012-12-26 01:27:41.129397"}, {"id"=>4, "description"=>"freezing", "created_at"=>"2012-12-26 01:27:41.284936", "updated_at"=>"2012-12-26 01:27:41.284936", 0=>4, 1=>"freezing", 2=>"2012-12-26 01:27:41.284936", 3=>"2012-12-26 01:27:41.284936"}] 1.9.3-p327 :003 > ActiveRecord::Base.connection.execute("SELECT * FROM states") (0.4ms) SELECT * FROM states => [{"id"=>1, "description"=>"running", "created_at"=>"2012-12-26 01:27:40.452140", "updated_at"=>"2012-12-26 01:27:40.452140", 0=>1, 1=>"running", 2=>"2012-12-26 01:27:40.452140", 3=>"2012-12-26 01:27:40.452140"}, {"id"=>2, "description"=>"stopping", "created_at"=>"2012-12-26 01:27:40.536188", "updated_at"=>"2012-12-26 01:27:40.536188", 0=>2, 1=>"stopping", 2=>"2012-12-26 01:27:40.536188", 3=>"2012-12-26 01:27:40.536188"}, {"id"=>3, "description"=>"restarting", "created_at"=>"2012-12-26 01:27:40.613507", "updated_at"=>"2012-12-26 01:27:40.613507", 0=>3, 1=>"restarting", 2=>"2012-12-26 01:27:40.613507", 3=>"2012-12-26 01:27:40.613507"}, {"id"=>4, "description"=>"creating", "created_at"=>"2012-12-26 01:27:40.688855", "updated_at"=>"2012-12-26 01:27:40.688855", 0=>4, 1=>"creating", 2=>"2012-12-26 01:27:40.688855", 3=>"2012-12-26 01:27:40.688855"}, {"id"=>5, "description"=>"destroying", "created_at"=>"2012-12-26 01:27:40.768293", "updated_at"=>"2012-12-26 01:27:40.768293", 0=>5, 1=>"destroying", 2=>"2012-12-26 01:27:40.768293", 3=>"2012-12-26 01:27:40.768293"}] 1.9.3-p327 :004 > Thanks Julien Estebane -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/MDSITqJnLl8J. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Dec-26 13:27 UTC
Re: has_to and belongs_to realtionships are not showing up in database
On Dec 26, 2:25 am, Julien Estebane <julienesteb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.> > I ran rake db:migrate before going to the rails console and checking the > tables... > > MODEL: > ------ > > user@user-DevMachine:~/dev/myapp/app/models$ cat engine.rb > class Engine < ActiveRecord::Base > # attr_accessible :title, :body > belongs_to :environment > belongs_to :state > end > user@user-DevMachine:~/dev/myapp/app/models$ cat environment.rb > class Environment < ActiveRecord::Base > attr_accessible :description > has_many :engines > end > user@user-DevMachine:~/dev/myapp/app/models$ cat state.rb > class State < ActiveRecord::Base > attr_accessible :description > has_many :engines > end > > MIGRATION: > ---------- > > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > 20121225230016_create_engines.rb > class CreateEngines < ActiveRecord::Migration > def change > create_table :engines do |t| > > t.timestamps > end > end > end > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > 20121225230034_create_states.rb > class CreateStates < ActiveRecord::Migration > def change > create_table :states do |t| > t.string :description > > t.timestamps > end > end > end > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > 20121225230118_create_environments.rb > class CreateEnvironments < ActiveRecord::Migration > def change > create_table :environments do |t| > t.string :description > t.timestamps > end > end > end > user@user-DevMachine:~/dev/myapp/db/migrate$ > > DATABASE: > --------- > > user@user-DevMachine:~/dev/myapp/db/migrate$ rails console > Loading development environment (Rails 3.2.9) > 1.9.3-p327 :001 > ActiveRecord::Base.connection.execute("SELECT * FROM > engines") > (1.1ms) SELECT * FROM engines > => [{"id"=>1, "created_at"=>"2012-12-26 01:26:38.789476", > "updated_at"=>"2012-12-26 01:26:38.789476", 0=>1, 1=>"2012-12-26 > 01:26:38.789476", 2=>"2012-12-26 01:26:38.789476"}] > 1.9.3-p327 :002 > ActiveRecord::Base.connection.execute("SELECT * FROM > environments") > (0.3ms) SELECT * FROM environments > => [{"id"=>1, "description"=>"warm", "created_at"=>"2012-12-26 > 01:27:40.968920", "updated_at"=>"2012-12-26 01:27:40.968920", 0=>1, > 1=>"warm", 2=>"2012-12-26 01:27:40.968920", 3=>"2012-12-26 > 01:27:40.968920"}, {"id"=>2, "description"=>"cold", > "created_at"=>"2012-12-26 01:27:41.055711", "updated_at"=>"2012-12-26 > 01:27:41.055711", 0=>2, 1=>"cold", 2=>"2012-12-26 01:27:41.055711", > 3=>"2012-12-26 01:27:41.055711"}, {"id"=>3, "description"=>"hot", > "created_at"=>"2012-12-26 01:27:41.129397", "updated_at"=>"2012-12-26 > 01:27:41.129397", 0=>3, 1=>"hot", 2=>"2012-12-26 01:27:41.129397", > 3=>"2012-12-26 01:27:41.129397"}, {"id"=>4, "description"=>"freezing", > "created_at"=>"2012-12-26 01:27:41.284936", "updated_at"=>"2012-12-26 > 01:27:41.284936", 0=>4, 1=>"freezing", 2=>"2012-12-26 01:27:41.284936", > 3=>"2012-12-26 01:27:41.284936"}] > 1.9.3-p327 :003 > ActiveRecord::Base.connection.execute("SELECT * FROM > states") > (0.4ms) SELECT * FROM states > => [{"id"=>1, "description"=>"running", "created_at"=>"2012-12-26 > 01:27:40.452140", "updated_at"=>"2012-12-26 01:27:40.452140", 0=>1, > 1=>"running", 2=>"2012-12-26 01:27:40.452140", 3=>"2012-12-26 > 01:27:40.452140"}, {"id"=>2, "description"=>"stopping", > "created_at"=>"2012-12-26 01:27:40.536188", "updated_at"=>"2012-12-26 > 01:27:40.536188", 0=>2, 1=>"stopping", 2=>"2012-12-26 01:27:40.536188", > 3=>"2012-12-26 01:27:40.536188"}, {"id"=>3, "description"=>"restarting", > "created_at"=>"2012-12-26 01:27:40.613507", "updated_at"=>"2012-12-26 > 01:27:40.613507", 0=>3, 1=>"restarting", 2=>"2012-12-26 01:27:40.613507", > 3=>"2012-12-26 01:27:40.613507"}, {"id"=>4, "description"=>"creating", > "created_at"=>"2012-12-26 01:27:40.688855", "updated_at"=>"2012-12-26 > 01:27:40.688855", 0=>4, 1=>"creating", 2=>"2012-12-26 01:27:40.688855", > 3=>"2012-12-26 01:27:40.688855"}, {"id"=>5, "description"=>"destroying", > "created_at"=>"2012-12-26 01:27:40.768293", "updated_at"=>"2012-12-26 > 01:27:40.768293", 0=>5, 1=>"destroying", 2=>"2012-12-26 01:27:40.768293", > 3=>"2012-12-26 01:27:40.768293"}] > 1.9.3-p327 :004 > > > Thanks > > Julien Estebane-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Julien Estebane
2012-Dec-26 18:19 UTC
Re: has_to and belongs_to realtionships are not showing up in database
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 `<main>'' Tasks: TOP => db:migrate (See full trace by running task with --trace) On Wednesday, December 26, 2012 8:27:27 AM UTC-5, Frederick Cheung wrote:> > > > On Dec 26, 2:25 am, Julien Estebane <julienesteb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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. > > > > I ran rake db:migrate before going to the rails console and checking the > > tables... > > > > MODEL: > > ------ > > > > user@user-DevMachine:~/dev/myapp/app/models$ cat engine.rb > > class Engine < ActiveRecord::Base > > # attr_accessible :title, :body > > belongs_to :environment > > belongs_to :state > > end > > user@user-DevMachine:~/dev/myapp/app/models$ cat environment.rb > > class Environment < ActiveRecord::Base > > attr_accessible :description > > has_many :engines > > end > > user@user-DevMachine:~/dev/myapp/app/models$ cat state.rb > > class State < ActiveRecord::Base > > attr_accessible :description > > has_many :engines > > end > > > > MIGRATION: > > ---------- > > > > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > > 20121225230016_create_engines.rb > > class CreateEngines < ActiveRecord::Migration > > def change > > create_table :engines do |t| > > > > t.timestamps > > end > > end > > end > > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > > 20121225230034_create_states.rb > > class CreateStates < ActiveRecord::Migration > > def change > > create_table :states do |t| > > t.string :description > > > > t.timestamps > > end > > end > > end > > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > > 20121225230118_create_environments.rb > > class CreateEnvironments < ActiveRecord::Migration > > def change > > create_table :environments do |t| > > t.string :description > > t.timestamps > > end > > end > > end > > user@user-DevMachine:~/dev/myapp/db/migrate$ > > > > DATABASE: > > --------- > > > > user@user-DevMachine:~/dev/myapp/db/migrate$ rails console > > Loading development environment (Rails 3.2.9) > > 1.9.3-p327 :001 > ActiveRecord::Base.connection.execute("SELECT * FROM > > engines") > > (1.1ms) SELECT * FROM engines > > => [{"id"=>1, "created_at"=>"2012-12-26 01:26:38.789476", > > "updated_at"=>"2012-12-26 01:26:38.789476", 0=>1, 1=>"2012-12-26 > > 01:26:38.789476", 2=>"2012-12-26 01:26:38.789476"}] > > 1.9.3-p327 :002 > ActiveRecord::Base.connection.execute("SELECT * FROM > > environments") > > (0.3ms) SELECT * FROM environments > > => [{"id"=>1, "description"=>"warm", "created_at"=>"2012-12-26 > > 01:27:40.968920", "updated_at"=>"2012-12-26 01:27:40.968920", 0=>1, > > 1=>"warm", 2=>"2012-12-26 01:27:40.968920", 3=>"2012-12-26 > > 01:27:40.968920"}, {"id"=>2, "description"=>"cold", > > "created_at"=>"2012-12-26 01:27:41.055711", "updated_at"=>"2012-12-26 > > 01:27:41.055711", 0=>2, 1=>"cold", 2=>"2012-12-26 01:27:41.055711", > > 3=>"2012-12-26 01:27:41.055711"}, {"id"=>3, "description"=>"hot", > > "created_at"=>"2012-12-26 01:27:41.129397", "updated_at"=>"2012-12-26 > > 01:27:41.129397", 0=>3, 1=>"hot", 2=>"2012-12-26 01:27:41.129397", > > 3=>"2012-12-26 01:27:41.129397"}, {"id"=>4, "description"=>"freezing", > > "created_at"=>"2012-12-26 01:27:41.284936", "updated_at"=>"2012-12-26 > > 01:27:41.284936", 0=>4, 1=>"freezing", 2=>"2012-12-26 01:27:41.284936", > > 3=>"2012-12-26 01:27:41.284936"}] > > 1.9.3-p327 :003 > ActiveRecord::Base.connection.execute("SELECT * FROM > > states") > > (0.4ms) SELECT * FROM states > > => [{"id"=>1, "description"=>"running", "created_at"=>"2012-12-26 > > 01:27:40.452140", "updated_at"=>"2012-12-26 01:27:40.452140", 0=>1, > > 1=>"running", 2=>"2012-12-26 01:27:40.452140", 3=>"2012-12-26 > > 01:27:40.452140"}, {"id"=>2, "description"=>"stopping", > > "created_at"=>"2012-12-26 01:27:40.536188", "updated_at"=>"2012-12-26 > > 01:27:40.536188", 0=>2, 1=>"stopping", 2=>"2012-12-26 01:27:40.536188", > > 3=>"2012-12-26 01:27:40.536188"}, {"id"=>3, "description"=>"restarting", > > "created_at"=>"2012-12-26 01:27:40.613507", "updated_at"=>"2012-12-26 > > 01:27:40.613507", 0=>3, 1=>"restarting", 2=>"2012-12-26 > 01:27:40.613507", > > 3=>"2012-12-26 01:27:40.613507"}, {"id"=>4, "description"=>"creating", > > "created_at"=>"2012-12-26 01:27:40.688855", "updated_at"=>"2012-12-26 > > 01:27:40.688855", 0=>4, 1=>"creating", 2=>"2012-12-26 01:27:40.688855", > > 3=>"2012-12-26 01:27:40.688855"}, {"id"=>5, "description"=>"destroying", > > "created_at"=>"2012-12-26 01:27:40.768293", "updated_at"=>"2012-12-26 > > 01:27:40.768293", 0=>5, 1=>"destroying", 2=>"2012-12-26 > 01:27:40.768293", > > 3=>"2012-12-26 01:27:40.768293"}] > > 1.9.3-p327 :004 > > > > > Thanks > > > > Julien Estebane >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/cL5LzuV2DpIJ. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Dec-26 19:27 UTC
Re: has_to and belongs_to realtionships are not showing up in database
On Dec 26, 6:19 pm, Julien Estebane <julienesteb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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> 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_rec ord/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_rec ord/migration.rb:466:in > `block in method_missing'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/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_rec ord/migration.rb:438:in > `say_with_time'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/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_rec ord/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_rec ord/migration.rb:407:in > `block in migrate'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/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_rec ord/migration.rb:389:in > `migrate'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/migration.rb:528:in > `migrate'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/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_rec ord/migration.rb:775:in > `call'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/migration.rb:775:in > `block in ddl_transaction'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/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_rec ord/transactions.rb:208:in > `transaction'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/migration.rb:775:in > `ddl_transaction'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/migration.rb:719:in > `block in migrate'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/migration.rb:700:in > `each'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/migration.rb:700:in > `migrate'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/migration.rb:570:in > `up'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/migration.rb:551:in > `migrate'' > /home/user/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.9/lib/active_rec ord/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 `<main>'' > Tasks: TOP => db:migrate > (See full trace by running task with --trace) > > > > > > > > On Wednesday, December 26, 2012 8:27:27 AM UTC-5, Frederick Cheung wrote: > > > On Dec 26, 2:25 am, Julien Estebane <julienesteb...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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. > > > > I ran rake db:migrate before going to the rails console and checking the > > > tables... > > > > MODEL: > > > ------ > > > > user@user-DevMachine:~/dev/myapp/app/models$ cat engine.rb > > > class Engine < ActiveRecord::Base > > > # attr_accessible :title, :body > > > belongs_to :environment > > > belongs_to :state > > > end > > > user@user-DevMachine:~/dev/myapp/app/models$ cat environment.rb > > > class Environment < ActiveRecord::Base > > > attr_accessible :description > > > has_many :engines > > > end > > > user@user-DevMachine:~/dev/myapp/app/models$ cat state.rb > > > class State < ActiveRecord::Base > > > attr_accessible :description > > > has_many :engines > > > end > > > > MIGRATION: > > > ---------- > > > > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > > > 20121225230016_create_engines.rb > > > class CreateEngines < ActiveRecord::Migration > > > def change > > > create_table :engines do |t| > > > > t.timestamps > > > end > > > end > > > end > > > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > > > 20121225230034_create_states.rb > > > class CreateStates < ActiveRecord::Migration > > > def change > > > create_table :states do |t| > > > t.string :description > > > > t.timestamps > > > end > > > end > > > end > > > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > > > 20121225230118_create_environments.rb > > > class CreateEnvironments < ActiveRecord::Migration > > ... > > read more »-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Julien Estebane
2012-Dec-27 00:01 UTC
Re: has_to and belongs_to realtionships are not showing up in database
upon further investigation and a lot of googling, i have found that in order to get environment_id and state_id columns into the engines table, I have to add t.belongs_to :environment t.belongs_to :state into the migrations files. Thanks for the assistance Frederic. :) On Tuesday, December 25, 2012 9:25:17 PM UTC-5, Julien Estebane wrote:> > 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 ? > > I ran rake db:migrate before going to the rails console and checking the > tables... > > MODEL: > ------ > > user@user-DevMachine:~/dev/myapp/app/models$ cat engine.rb > class Engine < ActiveRecord::Base > # attr_accessible :title, :body > belongs_to :environment > belongs_to :state > end > user@user-DevMachine:~/dev/myapp/app/models$ cat environment.rb > class Environment < ActiveRecord::Base > attr_accessible :description > has_many :engines > end > user@user-DevMachine:~/dev/myapp/app/models$ cat state.rb > class State < ActiveRecord::Base > attr_accessible :description > has_many :engines > end > > > > MIGRATION: > ---------- > > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > 20121225230016_create_engines.rb > class CreateEngines < ActiveRecord::Migration > def change > create_table :engines do |t| > > t.timestamps > end > end > end > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > 20121225230034_create_states.rb > class CreateStates < ActiveRecord::Migration > def change > create_table :states do |t| > t.string :description > > t.timestamps > end > end > end > user@user-DevMachine:~/dev/myapp/db/migrate$ cat > 20121225230118_create_environments.rb > class CreateEnvironments < ActiveRecord::Migration > def change > create_table :environments do |t| > t.string :description > t.timestamps > end > end > end > user@user-DevMachine:~/dev/myapp/db/migrate$ > > > > DATABASE: > --------- > > user@user-DevMachine:~/dev/myapp/db/migrate$ rails console > Loading development environment (Rails 3.2.9) > 1.9.3-p327 :001 > ActiveRecord::Base.connection.execute("SELECT * FROM > engines") > (1.1ms) SELECT * FROM engines > => [{"id"=>1, "created_at"=>"2012-12-26 01:26:38.789476", > "updated_at"=>"2012-12-26 01:26:38.789476", 0=>1, 1=>"2012-12-26 > 01:26:38.789476", 2=>"2012-12-26 01:26:38.789476"}] > 1.9.3-p327 :002 > ActiveRecord::Base.connection.execute("SELECT * FROM > environments") > (0.3ms) SELECT * FROM environments > => [{"id"=>1, "description"=>"warm", "created_at"=>"2012-12-26 > 01:27:40.968920", "updated_at"=>"2012-12-26 01:27:40.968920", 0=>1, > 1=>"warm", 2=>"2012-12-26 01:27:40.968920", 3=>"2012-12-26 > 01:27:40.968920"}, {"id"=>2, "description"=>"cold", > "created_at"=>"2012-12-26 01:27:41.055711", "updated_at"=>"2012-12-26 > 01:27:41.055711", 0=>2, 1=>"cold", 2=>"2012-12-26 01:27:41.055711", > 3=>"2012-12-26 01:27:41.055711"}, {"id"=>3, "description"=>"hot", > "created_at"=>"2012-12-26 01:27:41.129397", "updated_at"=>"2012-12-26 > 01:27:41.129397", 0=>3, 1=>"hot", 2=>"2012-12-26 01:27:41.129397", > 3=>"2012-12-26 01:27:41.129397"}, {"id"=>4, "description"=>"freezing", > "created_at"=>"2012-12-26 01:27:41.284936", "updated_at"=>"2012-12-26 > 01:27:41.284936", 0=>4, 1=>"freezing", 2=>"2012-12-26 01:27:41.284936", > 3=>"2012-12-26 01:27:41.284936"}] > 1.9.3-p327 :003 > ActiveRecord::Base.connection.execute("SELECT * FROM > states") > (0.4ms) SELECT * FROM states > => [{"id"=>1, "description"=>"running", "created_at"=>"2012-12-26 > 01:27:40.452140", "updated_at"=>"2012-12-26 01:27:40.452140", 0=>1, > 1=>"running", 2=>"2012-12-26 01:27:40.452140", 3=>"2012-12-26 > 01:27:40.452140"}, {"id"=>2, "description"=>"stopping", > "created_at"=>"2012-12-26 01:27:40.536188", "updated_at"=>"2012-12-26 > 01:27:40.536188", 0=>2, 1=>"stopping", 2=>"2012-12-26 01:27:40.536188", > 3=>"2012-12-26 01:27:40.536188"}, {"id"=>3, "description"=>"restarting", > "created_at"=>"2012-12-26 01:27:40.613507", "updated_at"=>"2012-12-26 > 01:27:40.613507", 0=>3, 1=>"restarting", 2=>"2012-12-26 01:27:40.613507", > 3=>"2012-12-26 01:27:40.613507"}, {"id"=>4, "description"=>"creating", > "created_at"=>"2012-12-26 01:27:40.688855", "updated_at"=>"2012-12-26 > 01:27:40.688855", 0=>4, 1=>"creating", 2=>"2012-12-26 01:27:40.688855", > 3=>"2012-12-26 01:27:40.688855"}, {"id"=>5, "description"=>"destroying", > "created_at"=>"2012-12-26 01:27:40.768293", "updated_at"=>"2012-12-26 > 01:27:40.768293", 0=>5, 1=>"destroying", 2=>"2012-12-26 01:27:40.768293", > 3=>"2012-12-26 01:27:40.768293"}] > 1.9.3-p327 :004 > > > > Thanks > > Julien Estebane >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/SjKXmp9zj8QJ. For more options, visit https://groups.google.com/groups/opt_out.