相同的协会两次,不同的名称,第二次使用错误的列

时间:2013-04-27 04:57:44

标签: ruby-on-rails activerecord model-associations

我正在尝试创建两个性质相同但有两个不同名称的关联,因为它们意味着不同的东西。我有一种感觉,这是一个语法问题,我不能完全正确。我宁愿不重写基础模型,但我愿意接受建议。就模型而言:Raids有玩家。突袭有领导者(是球员)。我希望这些玩家能够在players_raids表和leaders_raids表中进行raid关联。两组方法调用需要互补:players.raids和raids.players,以及players.raids_led和raids.leaders。

player.rb:

class Player < ActiveRecord::Base
  has_many :players_raids, class_name: 'PlayersRaids'
  has_many :raids, through: :players_raids
  has_many :leaders_raids, class_name: 'LeadersRaids'
  has_many :raids_led, through: :leaders_raids, source: :leader, foreign_key: 'leader_id'
end

raid.rb:

class Raid < ActiveRecord::Base
  has_many :players_raids, class_name: 'PlayersRaids'
  has_many :players, through: :players_raids
  has_many :leaders_raids, class_name: 'LeadersRaids'
  has_many :leaders, through: :leaders_raids
end

players_raids.rb:

class PlayersRaids < ActiveRecord::Base
  belongs_to :player
  belongs_to :raid
end

leaders_raids.rb:

class LeadersRaids < ActiveRecord::Base
  belongs_to :leader, class_name: 'Player'
  belongs_to :raid
end

create_players_raids.rb

class CreatePlayersRaids < ActiveRecord::Migration
  def change
    create_table :players_raids, id: false do |t|
      t.references :player
      t.references :raid
      t.timestamps
    end
  end
end

creates_leaders_raids.rb

class CreateLeadersRaids < ActiveRecord::Migration
  def change
    create_table :leaders_raids, id: false do |t|
      t.integer :leader_id
      t.references :raid
      t.timestamps
    end
  end
end

raids_controller.rb

class RaidsController < ApplicationController
  def create
    @raid = Raid.new(params[:raid])
    @raid.leaders << current_player
    @raid.players << current_player
    if @raid.save
      redirect_to raid_path(@raid), notice: "#{@raid.name} created!"
    else
      render 'new'
    end
  end
end

其中一些部分是我一直在玩的区域,即Player模型中'raids_led'关联的'source'和'foreign_key'字段以及当前't.integer的名称/引用: LeadersRaids表迁移文件中的leader_id'。

创建新的raid时,current_player(我的ApplicationController版本的current_user作为我的播放器模型是更常见的用户模型)应该作为新创建的raid的播放器和领导者添加。当我使用raid.players,raid.leaders和player.raids时,我完全恢复了我的期望。但是,当我使用player.raids_led时,我得到一个错误,它正在尝试使用raids_leaders.player_id而不是raids_leaders.leader_id(ActiveRecord :: StatementInvalid:Mysql2 :: Error:'where子句中的未知列'leaders_raids.player_id':SELECT players。*来自players内部加入leaders_raids playersid = leaders_raidsleader_id WHERE leaders_raidsplayer_id = 1)。任何人都可以帮我解决我的语法缺乏的问题吗?

修改 我更新了这些模型,它看起来像我原来的意图一样。也许这只是一些睡眠和新观点。如果有更好的方法,请随时更新或评论。

player.rb:

class Player < ActiveRecord::Base
  has_many :characters
  has_many :players_raids, class_name: 'PlayersRaids'
  has_many :raids, through: :players_raids
  has_many :leaders_raids, class_name: 'LeadersRaids'
  has_many :raids_led, through: :leaders_raids, source: :raid
end

raid.rb:

class Raid < ActiveRecord::Base
  has_many :players_raids, class_name: 'PlayersRaids'
  has_many :players, through: :players_raids
  has_many :leaders_raids, class_name: 'LeadersRaids'
  has_many :leaders, through: :leaders_raids, source: :leader
end

leaders_raids.rb:

class LeadersRaids < ActiveRecord::Base
  belongs_to :leader, class_name: 'Player', foreign_key: 'player_id'
  belongs_to :raid
end

create_leaders_raids.rb:

class CreateLeadersRaids < ActiveRecord::Migration
  def change
    create_table :leaders_raids, id: false do |t|
      t.integer :player_id
      t.references :raid
      t.timestamps
    end
  end
end

0 个答案:

没有答案