Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 对于新对象,Build有很多:通过_Ruby On Rails_Ruby On Rails 3_Activerecord_Has Many Through - Fatal编程技术网

Ruby on rails 对于新对象,Build有很多:通过

Ruby on rails 对于新对象,Build有很多:通过,ruby-on-rails,ruby-on-rails-3,activerecord,has-many-through,Ruby On Rails,Ruby On Rails 3,Activerecord,Has Many Through,总的来说,我可能做错了,但也许有人能进来帮忙 问题: 我希望能够在未保存的对象上建立关系,这样就可以: v = Video.new v.title = "New Video" v.actors.build(:name => "Jonny Depp") v.save! 此外,这些将通过一个自定义方法生成,我正在尝试对其进行修改以使其正常工作,该方法执行以下操作: v = Video.new v.title = "Interesting cast..." v.actors_list = "J

总的来说,我可能做错了,但也许有人能进来帮忙

问题

我希望能够在未保存的对象上建立关系,这样就可以:

v = Video.new
v.title = "New Video"
v.actors.build(:name => "Jonny Depp")
v.save!
此外,这些将通过一个自定义方法生成,我正在尝试对其进行修改以使其正常工作,该方法执行以下操作:

v = Video.new
v.title = "Interesting cast..."
v.actors_list = "Jonny Depp, Clint Eastwood, Rick Moranis"
v.save
此方法与video.rb中的类似

def actors_list=value
  #Clear for existing videos
  self.actors.clear

  value.split(',').each do |actorname|
    if existing = Actor.find_by_name(actorname.strip)
      self.actors << existing
    else
      self.actors.build(:name => actorname.strip)
    end
  end
end
has_many :actor_on_videos
has_many :actors, :through => :actor_on_videos

accepts_nested_attributes_for :actors
不幸的是,这些策略既不能创造演员,也不能创造联想。哦,是的,你可能会问我:

在video.rb中

def actors_list=value
  #Clear for existing videos
  self.actors.clear

  value.split(',').each do |actorname|
    if existing = Actor.find_by_name(actorname.strip)
      self.actors << existing
    else
      self.actors.build(:name => actorname.strip)
    end
  end
end
has_many :actor_on_videos
has_many :actors, :through => :actor_on_videos

accepts_nested_attributes_for :actors
我还尝试修改
actors\u list=
方法,如下所示:

def actors_list=value
  #Clear for existing videos
  self.actors.clear

  value.split(',').each do |actorname|
    if existing = Actor.find_by_name(actorname.strip)
      self.actors << existing
    else
      self.actors << Actor.create!(:name => actorname.strip)
    end
  end
end
def actors\u list=值
#清除现有视频
自我介绍,明白
value.split(',')。每个都做| actorname|
如果存在=Actor.find\u by\u name(actorname.strip)

self.actors您不能将子对象分配给没有id的对象。您需要将演员存储在新视频对象中,并在视频保存并具有id后保存这些记录。

您不能将子对象分配给没有id的对象。您需要将演员存储在新视频对象中,并在视频结束后保存这些记录已保存并具有id。

请尝试以下操作:

class Video < ActiveRecord::Base

  has_many :actor_on_videos
  has_many :actors, :through => :actor_on_videos

  attr_accessor :actors_list
  after_save    :save_actors

  def actors_list=names
    (names.presence || "").split(",").uniq.map(&:strip).tap do |new_list|
      @actors_list = new_list if actors_list_changes?(new_list)
    end
  end

  def actors_list_changes?(new_list)
    new_record? or 
      (actors.count(:conditions => {:name => new_list}) != new_list.size)
  end

  # save the actors in after save.
  def save_actors
    return true if actors_list.blank?
    # create the required names
    self.actors = actors_list.map {|name| Actor.find_or_create_by_name(name)}
    true
  end
end
class-Video:演员在视频中
属性访问器:参与者列表
保存后:保存演员
def actors_list=名称
(names.presence | | |“”).split(“,”).uniq.map(&:strip)。点击do |新建列表|
@演员名单=演员名单发生变化时的新名单?(新名单)
结束
结束
def参与者列表更改?(新列表)
新纪录?或
(actors.count(:conditions=>{:name=>new_list})!=new_list.size)
结束
#在保存之后保存演员。
def save_演员
如果actors\u list.blank返回true?
#创建所需的名称
self.actors=actors_list.map{| name | Actor.find_或_create_by_name(name)}
真的
结束
结束
试试这个:

class Video < ActiveRecord::Base

  has_many :actor_on_videos
  has_many :actors, :through => :actor_on_videos

  attr_accessor :actors_list
  after_save    :save_actors

  def actors_list=names
    (names.presence || "").split(",").uniq.map(&:strip).tap do |new_list|
      @actors_list = new_list if actors_list_changes?(new_list)
    end
  end

  def actors_list_changes?(new_list)
    new_record? or 
      (actors.count(:conditions => {:name => new_list}) != new_list.size)
  end

  # save the actors in after save.
  def save_actors
    return true if actors_list.blank?
    # create the required names
    self.actors = actors_list.map {|name| Actor.find_or_create_by_name(name)}
    true
  end
end
class-Video:演员在视频中
属性访问器:参与者列表
保存后:保存演员
def actors_list=名称
(names.presence | | |“”).split(“,”).uniq.map(&:strip)。点击do |新建列表|
@演员名单=演员名单发生变化时的新名单?(新名单)
结束
结束
def参与者列表更改?(新列表)
新纪录?或
(actors.count(:conditions=>{:name=>new_list})!=new_list.size)
结束
#在保存之后保存演员。
def save_演员
如果actors\u list.blank返回true?
#创建所需的名称
self.actors=actors_list.map{| name | Actor.find_或_create_by_name(name)}
真的
结束
结束

非常感谢!工作得很好。我给了一些编辑建议,关于我必须在哪里进行更改才能让它工作,我想你可以通过你的代表看到它们,对不起。我自己还没上去呢。给你>>@KyleMacey,我已经改变了
uniq
strip
的位置。此外,我还恢复了您对
save\u actors
方法所做的更改。它应该与原始代码一起工作。您遇到了什么错误?未定义参与者列表。。。我想这就是为什么你把它定义为@actors\u list,这样它就可以用在其他方法中。。。此外,如果在创建模型时没有传递actors\u列表(主要用于我在控制台中测试时),那么actors\u列表将在save\u actors中未定义,因为从未调用actors\u列表来定义它
actor\u列表
属性访问器将工作,即使未设置
@actor\u列表
变量。它将返回
nil
empty?
方法执行
nil
empty
检查,因此无需再次执行
nil
检查。您不应该在
setter
getter
函数之外访问属性变量,因为它破坏了数据封装规则。非常感谢!工作得很好。我给了一些编辑建议,关于我必须在哪里进行更改才能让它工作,我想你可以通过你的代表看到它们,对不起。我自己还没上去呢。给你>>@KyleMacey,我已经改变了
uniq
strip
的位置。此外,我还恢复了您对
save\u actors
方法所做的更改。它应该与原始代码一起工作。您遇到了什么错误?未定义参与者列表。。。我想这就是为什么你把它定义为@actors\u list,这样它就可以用在其他方法中。。。此外,如果在创建模型时没有传递actors\u列表(主要用于我在控制台中测试时),那么actors\u列表将在save\u actors中未定义,因为从未调用actors\u列表来定义它
actor\u列表
属性访问器将工作,即使未设置
@actor\u列表
变量。它将返回
nil
empty?
方法执行
nil
empty
检查,因此无需再次执行
nil
检查。您不应该在
setter
getter
函数之外访问属性变量,因为它破坏了数据封装规则。