Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.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 Rails:使用collection\u singular\u ids=ids方法保存令牌时指定关联类型_Ruby On Rails_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails Rails:使用collection\u singular\u ids=ids方法保存令牌时指定关联类型

Ruby on rails Rails:使用collection\u singular\u ids=ids方法保存令牌时指定关联类型,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,下面是我的模型。用语言来描述,一场“秀”有“剧组”“船员”由“人”组成。“剧组”中“表演”和“人物”之间的关联可以是“演员”或“其他”类型。 我有来自“Show”表单上的自动完成UI的代币,其中“人”填写在“Actor”字段或“Other”字段中 问题: 在“Show”模型中的actor\u tokens=(ids)和actor\u tokens=(ids)方法中,我在其中保存输入的令牌,如何将关系保存为“actor”或“other” 从actor\u令牌=(id)保存的所有关联应为“actor

下面是我的模型。用语言来描述,一场“秀”有“剧组”“船员”由“人”组成。“剧组”中“表演”和“人物”之间的关联可以是“演员”或“其他”类型。 我有来自“Show”表单上的自动完成UI的代币,其中“人”填写在“Actor”字段或“Other”字段中

问题: 在“Show”模型中的
actor\u tokens=(ids)
actor\u tokens=(ids)
方法中,我在其中保存输入的令牌,如何将关系保存为“actor”或“other”

actor\u令牌=(id)
保存的所有关联应为“actor”类型,并且 从
other_tokens=(id)
保存的所有关联应为“other”类型,并且

我正在寻找一个优雅的解决方案,以配合使用*collection\u singular\u ids=ids*来保存我的令牌

船员模型

class-Crew
人物模型

class-Person:剧组
结束
展示模型

class Show:船员
属性读取器:参与者标记
def actor_令牌=(id)
self.person\u id=id.split(“,”)
#----->将进入此处的标记关联为type='actor'
结束
def其他_令牌=(ID)
self.person\u id=id.split(“,”)
#----->将进入此处的令牌关联为type='other'
结束
结束
PS:请为这篇文章推荐一个更好的标题


谢谢

您不能使用
collection\u singular\u id=
以您想要的方式创建新的
Crew
实例。只接受有效的、已经存在的ID列表;不能通过此方法指定其他属性

相反,您可以构建
Crew
实例,根据需要指定
:person\u id
:type
,并将它们关联回
Show
的实例

def actor_tokens(ids)
  create_crews_from_ids(ids, :actor)
end

def other_tokens=(ids)
  create_crews_from_ids(ids, :other)
end

  private

  def create_crews_from_ids(ids, type)
    ids = ids.split(",").each do |id|
      crews.create({ person_id: id, type: type })
    end
  end

这是行不通的。我收到一个错误“除非保存了父对象,否则无法调用create”!您必须使用
new
而不是
create
来处理未持久化的
Show
?假设更新并保存了记录,那么旧令牌会发生什么情况?
class Person < ActiveRecord::Base
          attr_accessible :first_name, :handle, :last_name
          has_many :crews
          has_many :shows, :through => :crews

        end
 class Show < ActiveRecord::Base
      attr_accessible :handle, :name, :crew_tokens
      has_many :crews
      has_many :people, :through => :crews

      attr_reader :actor_tokens

      def actor_tokens=(ids)
        self.person_ids = ids.split(",")
            #----->associate tokens coming in here as type = 'actor'
      end

      def other_tokens=(ids)
        self.person_ids = ids.split(",")
            #----->associate tokens coming in here as type = 'other'
      end

    end 
def actor_tokens(ids)
  create_crews_from_ids(ids, :actor)
end

def other_tokens=(ids)
  create_crews_from_ids(ids, :other)
end

  private

  def create_crews_from_ids(ids, type)
    ids = ids.split(",").each do |id|
      crews.create({ person_id: id, type: type })
    end
  end