Has One
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 AddUserProfiles < ActiveRecord::Migration[7.0]
def change
create_table :user_profiles do |t|
t.bigint :user_id
end
add_foreign_key :user_profiles, :users, column: :user_id
# Adding this unique constraint(index) mark this relation-
# as "has_one" instead of "has_many".
add_index :user_profiles, [:user_id], unique: true
end
endResulting Model Relations
Last updated

