Когда у вас есть отношения многие ко многим, вам нужно явно создать промежуточную таблицу соединений? Как добавить внешние ключи для миграции? У меня есть модель пользователя и модель книги. У меня установлен иностранец . Вот мой код до сих пор. Может кто-нибудь показать мне, как действовать отсюда, пожалуйста?
USER
class User < ActiveRecord::Base
has_and_belongs_to_many :books
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
end
КНИЖНАЯ МОДЕЛЬ
class Book < ActiveRecord::Base
has_and_belongs_to_many :users
end
МИГРАЦИЯ ДЛЯ КНИГ
class CreateBooks < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.string :title
t.string :author
t.timestamps
end
end
def self.down
drop_table :books
end
end
МИГРАЦИЯ ДЛЯ ПОЛЬЗОВАТЕЛЯ
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
t.timestamps
end
add_index :users, :email, :unique => true
add_index :users, :reset_password_token, :unique => true
# add_index :users, :unlock_token, :unique => true
end
def self.down
drop_table :users
end
end