environment
Creating office utilization matching for inns I want to allow users to bookmark hotels In addition, hotel_favorite has already been modeled.
I want the hotel_favorite model to have user_id and hotel_id. (User_id is already associated)
rails g migration AddhotelidTohotel_favorites
→ XXXXXXX_add_hotel_ref_tohotel_favorites.rb was created. I edited it as follows.
XXXXXXX_add_hotel_ref_tohotel_favorites.rb
class AddHotelRefTohotelFavorites < ActiveRecord::Migration[6.0]
def change
add_reference :hotel_favorites, :hotel, null: false, foreign_key: true
end
end
here
rails db:migrate
Hotel_id has been added to hotel_favorite as shown below.
schema.rb
create_table "hotel_favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.bigint "user_id", null: false
t.bigint "hotel_id", null: false
t.index ["hotel_id"], name: "index_hotel_favorites_on_hotel_id"
t.index ["user_id"], name: "index_hotel_favorites_on_user_id"
From the next time, as in the article below, you should do it at the time of model creation. .. https://qiita.com/hiro266/items/8a0374155cd18adbd7cf
rails g model hotel_favorite user:references hotel:references
Recommended Posts