Я создаю в RoR веб-игру Fantasy Football, в которой пользователь может зарегистрироваться, создать клуб и играть в лиге со своими друзьями.
Я построил область разговоров для пользователей в том жеЛига, чтобы сообщать друг другу по отдельности и может заставить его работать со всеми пользователями (@users = User.all), однако я не уверен, как сделать ссылку на клубную таблицу, которая содержит пользователей league_id.
Как мне это сделать?
class User < ApplicationRecord
has_secure_password
has_many :messages
has_many :conversations, :primary_key => :sender_id
has_one :club
has_one :league_chat
end
class Club < ApplicationRecord
belongs_to :user
belongs_to :league
has_many :players
end
class League < ApplicationRecord
has_one :league_chat
has_many :clubs
has_many :users, through: :clubs
end
class ConversationsController < ApplicationController
# before_action :authenticate_user
before_action :user_confirm_logged_in
def index
@users = current_user.club.league.users
@conversations = Conversation.all
end
def create
if Conversation.between(params[:sender_id], params[:recipient_id]).present?
@conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
else
@conversation = Conversation.create!(conversation_params)
end
redirect_to conversation_messages_path(@conversation)
end
private
def conversation_params
params.permit(:sender_id, :recipient_id, :league_id)
end
end