Belongs To / Has Many
Preparing your DB schema
class AddUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
# "id" primary key column will also be added implicitly by Rails(ActiveRecord)
t.string :email, null: false
end
end
endclass AddPosts < ActiveRecord::Migration[7.0]
def change
create_table :posts do |t|
t.bigint :author_id
end
# Define foreign key to `users.id` column from own `posts.author_id` column.
add_foreign_key :posts, :users, column: :author_id
end
end
