Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.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将属性从用户对象复制到新对象_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails Rails将属性从用户对象复制到新对象

Ruby on rails Rails将属性从用户对象复制到新对象,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,在用户模型中,我有一个存档!方法,该方法在用户被销毁时调用。此操作在单独的表中创建一个新的ArchivedUser ArchivedUser已成功创建,但我手动设置每个值的方式非常糟糕;如果将新列添加到用户表中,则必须将其添加到此处 我试图选择和切片属性,但得到了未定义的局部变量或方法用户的` ArchivedUser.create(user.attributes.select{ |key, _| ArchivedUser.attribute_names.include? key }) Arc

在用户模型中,我有一个存档!方法,该方法在用户被销毁时调用。此操作在单独的表中创建一个新的ArchivedUser

ArchivedUser已成功创建,但我手动设置每个值的方式非常糟糕;如果将新列添加到用户表中,则必须将其添加到此处

我试图
选择
切片
属性,但得到了
未定义的局部变量或方法
用户的`

ArchivedUser.create(user.attributes.select{ |key, _| ArchivedUser.attribute_names.include? key })

ArchivedUser.create(user.attributes.slice(ArchivedUser.attribute_names))
在使用self创建ArchivedUser时,如何迭代用户表中的每个属性

def archive!
    if ArchivedUser.create(
        user_id: self.id,
        company_id: self.company_id,
        first_name: self.first_name,
        last_name: self.last_name,
        email: self.email,
        encrypted_password: self.encrypted_password,
        password_salt: self.password_salt,
        session_token: self.session_token,
        perishable_token: self.perishable_token,
        role: self.role,
        score: self.score,
        created_at: self.created_at,
        updated_at: self.updated_at,
        api_key: self.api_key,
        device_id: self.device_id,
        time_zone: self.time_zone,
        device_type: self.device_type,
        verified_at: self.verified_at,
        verification_key: self.verification_key,
        uninstalled: self.uninstalled,
        device_details: self.device_details,
        is_archived: self.is_archived,
        registered_at: self.registered_at,
        logged_in_at: self.logged_in_at,
        state: self.state,
        creation_state: self.creation_state,
        language_id: self.language_id,
        offer_count: self.offer_count,
        expired_device_id: self.expired_device_id,
        unique_id: self.unique_id,
        best_language_code: self.best_language_code,
        offer_id: self.offer_id,
        vetted_state: self.vetted_state,
        photo_path: self.photo_path
      )

      self.is_archived = true

      self.email = "#{self.email}.archived#{Time.now.to_i}"
      self.encrypted_password = nil
      self.password_salt      = nil
      self.session_token      = nil
      self.perishable_token   = nil
      self.device_id          = nil
      self.verification_key   = nil
      self.save!

      self.update_column(:api_key, nil)
      UserGroup.delete_all(:user_id => self.id)
    else
      # handle the ArchivedUser not being created properly
    end
  end
感谢收看:)

更新:

我们能够找出
ArchivedUser.create(self.attributes.slice!(ArchivedUser.attribute\u name)
不起作用的原因。第一个原因是
create
方法需要“bang”写入对象。第二个原因是ArchivedUser有一个用户id字段,该用户在创建后才接收。我们必须使用
merge(user\u id:self.id)

最终输出如下所示
ArchivedUser.create!(self.attributes.slice!(ArchivedUser.attribute\u names).merge(user\u id:self.id))
第一个实现的方法是正确的。只需使用self而不是user

ArchivedUser.create(self.attributes.slice(ArchivedUser.attribute_names))

如果您只想拥有一份正在存档的用户的副本,我认为一个优雅的方法是这样做

archived\u user=user\u to\u be\u archived.dup

或者你可以看看变形虫宝石,这将做所有的繁重工作,包括协会,如果你想。

谢谢Ehren…虽然这可以工作并显示flash消息,但ArchivedUser实际上并没有在ArchivedUser表中创建,这很奇怪。@VegaStudios好的,这可能是因为验证。尝试使用
create!
而不是
create
来获取验证消息的异常。Hmph,这引发了Validatio错误n失败:用户不能为空。我要做一些挖掘…是的!啊,你的ArchivedUser可能有一个用户字段,这是普通用户不会有的。你只需要在切片行之后再行设置它。只需
archived\u User=ArchivedUser.new();archived_user.user=self;archived_user.save!
Ah!我在不断地学习:谢谢你带我走过这段路。