Как я могу установить пользователей, которые находятся в одной лиге, в качестве переменной? - PullRequest
0 голосов
/ 11 июня 2018

Я создаю в 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

Ответы [ 3 ]

0 голосов
/ 11 июня 2018

Так что вы можете сделать

all_league_users = current_user.club.league.users

Это работает, если у вас есть ...

class League
  has_many :clubs
  has_many :users, through: :clubs
end

class Club
  belongs_to :league
end
0 голосов
/ 11 июня 2018
users = Club.where(league_id: the_league_you_want.id).all.map(&:user).uniq
0 голосов
/ 11 июня 2018

Следующее даст вам всех пользователей из одной лиги:

User.where(league_id: your_variable)

Замените your_variable фактической переменной, которая содержит id лиги, для которой вы хотите найти пользователей.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...