Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails:针对同一父子对的多个acts\U as\U list指令_Ruby On Rails_List_Parent Child - Fatal编程技术网

Ruby on rails RubyonRails:针对同一父子对的多个acts\U as\U list指令

Ruby on rails RubyonRails:针对同一父子对的多个acts\U as\U list指令,ruby-on-rails,list,parent-child,Ruby On Rails,List,Parent Child,我正在开发一个纸牌游戏服务器,用于每个玩家都有一副牌和一堆弃牌的游戏。为了反映一个事实,即一张给定的牌可能位于玩家的牌组或其弃牌堆中,我将我的玩家模型设置如下: class Player < ActiveRecord::Base belongs_to :game has_many :deck_cards, :class_name => "Card", :conditions => "location = 'deck'",

我正在开发一个纸牌游戏服务器,用于每个玩家都有一副牌和一堆弃牌的游戏。为了反映一个事实,即一张给定的牌可能位于玩家的牌组或其弃牌堆中,我将我的玩家模型设置如下:

class Player < ActiveRecord::Base
  belongs_to :game

  has_many :deck_cards, :class_name => "Card",
                        :conditions => "location = 'deck'",
                        :order => "position",
                        :dependent => :delete_all

  has_many :discard_cards, :class_name => "Card",
                           :conditions => "location = 'discard'",
                           :order => "position",
                           :dependent => :delete_all
end
classplayer“卡”,
:条件=>“位置='deck'”,
:order=>“position”,
:dependent=>:全部删除
拥有多张:弃牌,:class\u name=>“牌”,
:conditions=>“location='discard'”,
:order=>“position”,
:dependent=>:全部删除
结束

有没有可能定义我的
模型,使其
作为列表
每一张卡都在一个列表中(单个玩家的牌组或弃牌)?

你的问题不在于验证,更重要的是,作为列表不允许超过一个属于关系的列表

下面的验证将确保一张牌在玩家的弃牌或牌组列表中,但不能同时在两者中

validates_uniqueness_of [:fields, :that, :uniquely, :describe, :a, :card],
  :scope => [:player_id, :location]
你应该能够使用STI来实现你想要的多个列表。注意:这是未经测试的,但至少应该让您走上正确的轨道

class Card < ActiveRecord::Base
  belongs_to :player
  validates_uniqueness_of [:fields, :that, :uniquely, :describe, :a, :card],
    :scope => [:player_id, :location]
  set_inheritance_column :location
end

class DeckCard < Card
  acts_as_list, :scope => :player
end

class DiscardCard < Card
  acts_as_list, :scope => :player
end

class Player
  belongs_to :game
  has_many :deck_cards, :order => "position",
                        :dependent => :delete_all

  has_many :discard_cards, :order => "position",
                           :dependent => :delete_all
end
class-Card[:player_id,:location]
集合\继承\列:位置
结束
类别卡片<卡片
作为列表,作用域=>:player
结束
类别卡片<卡片
作为列表,作用域=>:player
结束
职业选手
属于:游戏
有很多:牌组牌,:订单=>“位置”,
:dependent=>:全部删除
有很多:弃牌,:订单=>“位置”,
:dependent=>:全部删除
结束

您的问题不在于验证,更重要的是,作为列表在单个“所属”关系上不允许超过个列表

下面的验证将确保一张牌在玩家的弃牌或牌组列表中,但不能同时在两者中

validates_uniqueness_of [:fields, :that, :uniquely, :describe, :a, :card],
  :scope => [:player_id, :location]
你应该能够使用STI来实现你想要的多个列表。注意:这是未经测试的,但至少应该让您走上正确的轨道

class Card < ActiveRecord::Base
  belongs_to :player
  validates_uniqueness_of [:fields, :that, :uniquely, :describe, :a, :card],
    :scope => [:player_id, :location]
  set_inheritance_column :location
end

class DeckCard < Card
  acts_as_list, :scope => :player
end

class DiscardCard < Card
  acts_as_list, :scope => :player
end

class Player
  belongs_to :game
  has_many :deck_cards, :order => "position",
                        :dependent => :delete_all

  has_many :discard_cards, :order => "position",
                           :dependent => :delete_all
end
class-Card[:player_id,:location]
集合\继承\列:位置
结束
类别卡片<卡片
作为列表,作用域=>:player
结束
类别卡片<卡片
作为列表,作用域=>:player
结束
职业选手
属于:游戏
有很多:牌组牌,:订单=>“位置”,
:dependent=>:全部删除
有很多:弃牌,:订单=>“位置”,
:dependent=>:全部删除
结束

我想这不是我想要的。我希望卡牌在玩家范围内使用acts_as_列表,无论是牌组还是弃牌。。。我想。嗯。这会不会让你很难,比如说,把牌从牌组的顶部移动到弃牌的顶部?验证确保一张牌只在一个列表中。然而,我并不熟悉acts_as_列表。该插件的来源并不建议您可以在同一关系上定义两个列表。解决方案已经更新,以反映这一点。我喜欢这一点-所以我将继续它-但遗憾的是,它没有帮助我。我的卡片有不同的类型,所以我已经在用STI来区分它们了。如果没有更好的结果出现,我也会在几天后接受这一点。属于卡片类型和牌手,并且具有列表类型。不幸的是,我没有时间给你举个例子。如果答案不适合你,也不要担心接受它。我的名声已经够多了。我想这不是我想要的。我希望卡牌在玩家范围内使用acts_as_列表,无论是牌组还是弃牌。。。我想。嗯。这会不会让你很难,比如说,把牌从牌组的顶部移动到弃牌的顶部?验证确保一张牌只在一个列表中。然而,我并不熟悉acts_as_列表。该插件的来源并不建议您可以在同一关系上定义两个列表。解决方案已经更新,以反映这一点。我喜欢这一点-所以我将继续它-但遗憾的是,它没有帮助我。我的卡片有不同的类型,所以我已经在用STI来区分它们了。如果没有更好的结果出现,我也会在几天后接受这一点。属于卡片类型和牌手,并且具有列表类型。不幸的是,我没有时间给你举个例子。如果答案不适合你,也不要担心接受它。我的名声已经够多了。