概要
1.物理削除式の退会機能を付けようとしています。
2.エラーが発生し、エラーの内容はroom.user_idというカラムはありませんと表示されています。
3.全roomテーブルの中で退会しようとしているユーザーのIDが入っているroomも削除するためEntryテーブルから
@currentUserEntry=Entry.where(user_id: current_user.id)
と記述しuser_idを取得しようとしました。
4.ルームの中にあるuser_idはEntryからとってくると思っているのだが正しいかは不明
!追記
関連定義を見直し、退会を行おうとしたところエラーが発生しました。
発生している問題・エラーメッセージ
ActiveRecord::HasManyThroughOrderError at /users/3/withdrawal Cannot have a has_many :through association 'User#rooms' which goes through 'User#entries' before the through association is defined.
def withdrawal @user = User.find(params[:id]) @user.destroy ←エラー箇所 flash[:notice] = 'ユーザーを削除しました。' redirect_to '/' end
該当のソースコード
恐らく関連している箇所はuserのコントローラとモデルの2つです。
users_controller.rb/withdrawalアクション
1def withdrawal 2 @user = User.find(params[:id]) 3 @user.destroy 4 flash[:notice] = 'ユーザーを削除しました。' 5 redirect_to '/' 6 end
user.rbのhas_many箇所
1has_many :plans, :dependent => :destroy 2 has_many :relationships, :dependent => :destroy 3 has_many :followings, through: :relationships, source: :follow, :dependent => :destroy 4 has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id', :dependent => :destroy 5 has_many :followers, through: :reverses_of_relationship, source: :user, :dependent => :destroy 6 has_many :favorites, :dependent => :destroy 7 has_many :favorite_plans, through: :favorites, source: :plan, :dependent => :destroy 8 has_many :rooms, through: :entries 9 has_many :rooms, through: :entries, :dependent => :destroy 10 has_many :entries, :dependent => :destroy 11 has_many :messages, :dependent => :destroy
!追記
schema/roomsandUsers
1create_table "rooms", charset: "utf8mb4", force: :cascade do |t| 2 t.string "room_name" 3 t.datetime "created_at", precision: 6, null: false 4 t.datetime "updated_at", precision: 6, null: false 5 t.bigint "plan_id", null: false 6 t.index ["plan_id"], name: "index_rooms_on_plan_id" 7 end 8 9create_table "users", charset: "utf8mb4", force: :cascade do |t| 10 t.string "email", default: "", null: false 11 t.string "encrypted_password", default: "", null: false 12 t.string "name", null: false 13 t.string "furiganaName", null: false 14 t.string "password_digest", default: "", null: false 15 t.string "telephone_number", null: false 16 t.string "post_code", null: false 17 t.string "address", null: false 18 t.string "self_introduction" 19 t.string "image" 20 t.string "reset_password_token" 21 t.datetime "reset_password_sent_at" 22 t.datetime "remember_created_at" 23 t.datetime "created_at", precision: 6, null: false 24 t.datetime "updated_at", precision: 6, null: false 25 t.boolean "is_deleted", default: true, null: false 26 t.index ["email"], name: "index_users_on_email", unique: true 27 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 28 end
room.rb
1class Room < ApplicationRecord 2 has_many :entries, dependent: :destroy 3 has_many :messages, dependent: :destroy 4 belongs_to :plan 5 has_many :users, through: :entries 6 has_many :users, through: :entries, :dependent => :destroy 7end 8
箇条書きで簡潔に書こうと試みてみたのですが、うまく伝わっていませんでしたらすみません。
!追記
・物理削除
退会処理が行われるとユーザーはアカウントを使用できなくなります。
そして、データ自体も記憶媒体(データベース)から削除されます。
・論理削除
退会処理が行われるとユーザーはアカウントを使用できなくなります。
ただし、データ自体は記憶媒体(データベース)には存在します。
!追記 コードの追記
_room.html.erb
1<div class = 'dm-list'> 2 <% @rooms.zip(@anotherEntries) do |room,entry| %> 3 <div style='width: 200px; border:solid 1px gray'> 4 <p><%= link_to entry.user.name, user_path(entry.user), class: "text-decoration-none" %></p> 5 <p><%= link_to room.room_name, plan_path(room.plan_id), class: "text-decoration-none" %></p> 6 <p><%= link_to 'メッセージ', room, class: "btn btn-primary btn-lg" %></p> 7 </div><br /> 8 <% end %> 9 <div class="d-flex justify-content-left mb-2"> 10 <%== pagy_bootstrap_nav(@pagy) %> 11 </div> 12</div> 13






回答1件
あなたの回答
tips
プレビュー