Currently, [Rails6] Create Slack-like chat app with Action Cable - Qiita article to create a chat room for asynchronous communication. I was able to create a chat room, but I get an error when I see
ActiveRecord::AssociationNotFoundError in Rooms#index
Association named 'user' was not found on Room; perhaps you misspelled it?
Extracted source (around line #9):
</div>
<%@rooms.each do | room | %>
<ul>
<%=link_to room_path(room), method::get do%>
<class="recommended_title">Title:<%=room.name%>>span class="share_comment_time">%=room.created_at.to_s(:datetime_jp)%>/span>>>>
We have created an intermediate table called room_user, where we associate it as follows:
Room_user Model
class RoomUser<ApplicationRecord
belongs_to —room
belongs_to —user
end
Room Model
class Room<ApplicationRecord
has_many —room_users
has_many:users,through::room_users,dependent::destroy
has_many:messages,dependent::destroy
def template
ApplicationController.render.renderpartial: 'messages/message', locals: {message:self}
end
end
User Model
has_many:room_users
has_many:rooms, through::room_users
has_many:messages,dependent::destroy
Rooms/Index.html.erb
<%=render "shared/header"%>
<body class="shares_body">
<h1class="share_title">CHAT ROOMS</h1>
<div class="share_btn_wrapper">
<%=link_to"◎Create a chat room◎", new_room_path, class: "share_btn"%>
</div>
<%@rooms.each do | room | %>
<ul>
<%=link_to room_path(room), method::get do%>
<class="recommended_title">Title:<%=room.name%>>span class="share_comment_time">%=room.created_at.to_s(:datetime_jp)%>/span>>>>
<%end%>
</ul>
<%end%>
<%=paginate@rooms%>
<div class="top_page_link">
<%=link_to "Back to top page", "/"%>
</div>
</body>
<%=render "shared/footer"%>
Room Controller
class RoomsController <ApplicationController
def index
@rooms=Room.all.includes(:user).order("created_at DESC").page(params[:page]).per(10)
end
def new
@room=Room.new
end
def create
@room=Room.new(room_params)
[email protected]
redirect_to rooms_path
else
render 'new'
end
end
def show
@messages=Message.all.includes(:user).order(:id)
@message=current_user.messages.build
end
private
defroom_params
param.require(:room).permit(:name,user_ids:[])
end
end
When I create a room, it is reflected in the column, so I made it appear in the Index view file, but the error I mentioned at the beginning appears.
I'm wondering if the association has been completed, but an association-related error occurred and I checked other files, but I didn't know where the error was, so I asked you a question to help me.
I've just started studying, and I'm sorry for the poor explanation and code, but I appreciate your cooperation.
I solved myself.
I don't think I needed .includes(:user) in the controller, so I don't think there's a user tied to the room, but I'm going to check it out myself a little bit more.
575 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
930 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
578 Who developed the "avformat-59.dll" that comes with FFmpeg?
624 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.