ruby-on-rails – 具有多个外键的Has_many,belongs_to
发布时间:2020-12-17 03:41:06 所属栏目:百科 来源:网络整理
导读:我试图通过has_many,belongs_to关系将比赛归因于俱乐部.但是,在比赛中,我需要将俱乐部设置为home_team或away_team.为了解决这个问题,我使用了两个foreign_keys. class Club ActiveRecord::Base has_many :matchesendclass Match ActiveRecord::Base belongs_
我试图通过has_many,belongs_to关系将比赛归因于俱乐部.但是,在比赛中,我需要将俱乐部设置为home_team或away_team.为了解决这个问题,我使用了两个foreign_keys.
class Club < ActiveRecord::Base has_many :matches end class Match < ActiveRecord::Base belongs_to :home_team,class_name: 'Club',foreign_key: 'home_team_id' belongs_to :away_team,foreign_key: 'away_team_id' end 这使用home_team_id和away_team_id在比赛中很好地设置了俱乐部. 但是,我无法通过Club.matches访问所有俱乐部的比赛. ERROR: column matches.club_id does not exist 我怎样才能改变我的关系,这样才能做到这一点? 解决方法
您可以定义外键
class Club < ActiveRecord::Base has_many :home_matches,class_name: 'Match',foreign_key: 'home_team_id' has_many :away_matches,foreign_key: 'away_team_id' end 但我怀疑这会导致更多问题,因为你可能想要获得所有匹配和按日期排序,你可以通过做两个查询并添加结果和排序来做,但坦率地说这很麻烦. 我最初认为你应该看着你希望能够做的很多关系@ club.matches class Club < ActiveRecord::Base has_many :club_matches has_many :matches,through: :club_matches end class ClubMatch < ActiveRecord::Base belongs_to :club belongs_to :match #will have an attribute on it to determine if home or away team end class Match < ActiveRecord::Base has_many :club_matches has_many :clubs,through: :club_matches end 然后你就可以做@ club.matches了 只是我最初的想法,有人可能会提出一个更好的解决方案 据推测,虽然你可以在没有关联的情况下进行查询,这可能会更好,而且不会为你重构.例如 class WhateverController < ApplicationController def matches @club = Club.find(params[:club_id) @matches = Match.where("home_team_id = :club_id OR away_team_id = :club_id",{club_id: @club.id}).order(:date) end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |