Я создал 2 таблицы в своем приложении rails: Contact
и Category
, используя ff:
rails g controller Contact name email
rails g model Category title
Обе уже были мигрировали. Как вы можете видеть, нет никакого отношения к этим двум. Мой вопрос: как мне установить отношение, чтобы в модели контактов было поле category_id
?
Есть ли для этого команда rails? или какую-нибудь команду миграции, которую я могу запустить?
ОБНОВЛЕНИЯ:
Я пытался выполнить это, но я получил эти ошибки ниже при rails db:migrate
:
rails aborted!
SyntaxError: /home/jojo/Desktop/contacthub/db/migrate/20200108110024_create_contacts.rb:5: syntax error, unexpected tSYMBEG, expecting end
t.string :email,
^
/home/jojo/Desktop/contacthub/db/migrate/20200108110024_create_contacts.rb:7: syntax error, unexpected tSYMBEG, expecting end
t.string :phone,
^
/home/jojo/Desktop/contacthub/db/migrate/20200108110024_create_contacts.rb:9: syntax error, unexpected tSYMBEG, expecting end
t.string :address,
^
/home/jojo/Desktop/contacthub/db/migrate/20200108110024_create_contacts.rb:11: syntax error, unexpected tSYMBEG, expecting end
t.string :state,
^
/home/jojo/Desktop/contacthub/db/migrate/20200108110024_create_contacts.rb:13: syntax error, unexpected tSYMBEG, expecting end
t.text :note
^
/home/jojo/Desktop/contacthub/db/migrate/20200108110024_create_contacts.rb:17: syntax error, unexpected end, expecting end-of-input
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `block in require'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:986:in `load_migration'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:982:in `migration'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:977:in `disable_ddl_transaction'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1350:in `use_transaction?'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1342:in `ddl_transaction'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1291:in `execute_migration_in_transaction'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1263:in `block in migrate_without_lock'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1262:in `each'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1262:in `migrate_without_lock'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1210:in `block in migrate'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1363:in `with_advisory_lock'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1210:in `migrate'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1036:in `up'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/migration.rb:1011:in `migrate'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/tasks/database_tasks.rb:172:in `migrate'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activerecord-5.2.4.1/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <main>'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/railties-5.2.4.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/railties-5.2.4.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/railties-5.2.4.1/lib/rails/command.rb:48:in `invoke'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/railties-5.2.4.1/lib/rails/commands.rb:18:in `<main>'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/bootsnap-1.4.5/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `block in require'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
/home/jojo/Desktop/contacthub/bin/rails:9:in `<top (required)>'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `load'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/spring-2.1.0/lib/spring/client/rails.rb:28:in `call'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/spring-2.1.0/lib/spring/client/command.rb:7:in `call'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/spring-2.1.0/lib/spring/client.rb:30:in `run'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/spring-2.1.0/bin/spring:49:in `<top (required)>'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `load'
/home/jojo/.rvm/gems/ruby-2.6.0/gems/spring-2.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
/home/jojo/Desktop/contacthub/bin/spring:15:in `require'
/home/jojo/Desktop/contacthub/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Вот файл миграции:
class CreateContacts < ActiveRecord::Migration[5.2]
def change
create_table :contacts do |t|
t.string :name,
t.string :email,
t.string :mobile,
t.string :phone,
t.string :country,
t.string :address,
t.string :city,
t.string :state,
t.string :zip,
t.text :note
t.references :category, foreign_key: true
end
end
end