ruby-on-rails - 重新分配 ActiveRecord 实例和相应的外键

标签 ruby-on-rails ruby-on-rails-3 activerecord

在 Rails/ActiveReocrd 中有一种方法可以用另一个实例替换一个实例,以便解决所有关系/外键。

我可以想象这样的事情:

//setup
customer1 = Customer.find(1)
customer2 = Customer.find(2)

//this would be cool
customer1.replace_with(customer2)

假设 customer1 配置不当,有人创建了 customer2,但不知道 customer1,能够快速将所有内容设置为 customer2 就好了

因此,这也需要更新任何外键

用户所属:客户
网站所属:客户

然后任何具有外键 customer_id = 1 的用户/网站将通过此 'replace_with' 方法自动设置为 2

这样的事情存在吗?

[我可以想象一个涉及 Customer.reflect_on_all_associations(:has_many) 等的黑客攻击]

干杯,
J

最佳答案

像这样的事情可以工作,尽管可能有更合适的方法:

更新 :更正了关联示例中的一些错误。

class MyModel < ActiveRecord::Base

  ...

  # if needed, force logout / expire session in controller beforehand.
  def replace_with (another_record)
    # handles attributes and belongs_to associations
    attribute_hash = another_record.attributes
    attribute_hash.delete('id')
    self.update_attributes!(attribute_hash)

    ### Begin association example, not complete.

    # generic way of finding model constants
    find_model_proc = Proc.new{ |x| x.to_s.singularize.camelize.constantize }
    model_constant = find_model_proc.call(self.class.name)

    # handle :has_one, :has_many associations
    have_ones = model_constant.reflect_on_all_associations(:has_one).find_all{|i| !i.options.include?(:through)}
    have_manys = model_constant.reflect_on_all_associations(:has_many).find_all{|i| !i.options.include?(:through)}

    update_assoc_proc = Proc.new do |assoc, associated_record, id|
      primary_key = assoc.primary_key_name.to_sym
      attribs = associated_record.attributes
      attribs[primary_key] = self.id
      associated_record.update_attributes!(attribs)
    end

    have_ones.each do |assoc|
      associated_record = self.send(assoc.name)
      unless associated_record.nil?
        update_assoc_proc.call(assoc, associated_record, self.id)
      end
    end

    have_manys.each do |assoc|
      associated_records = self.send(assoc.name)
      associated_records.each do |associated_record|
        update_assoc_proc.call(assoc, associated_record, self.id)
      end
    end

    ### End association example, not complete.

    # and if desired..
    # do not call :destroy if you have any associations set with :dependents => :destroy
    another_record.destroy
  end

  ...

end

我已经包含了一个关于如何处理某些关联的示例,但总的来说这可能会变得棘手。

关于ruby-on-rails - 重新分配 ActiveRecord 实例和相应的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7507878/

相关文章:

ruby-on-rails-3 - 茧,link_to_remove_association 与确认?

javascript - Rails Ajax 在保留参数的同时刷新部分内容

ruby-on-rails - rails : save doesn't update record if called twice?

javascript - 从 JSON API 响应解析包含的关联 - Rails API、AMS、Vue.js SPA

ruby-on-rails - 如何创建一个范围来查找 "Authors who have zero posts"?

ruby-on-rails - Rails3 结构、引擎、插件、切片、 gem 、模块..?

windows - Webrick 和 Thin 在 Windows 中提供静态文件的速度非常慢。我怎样才能加快他们的速度?

ruby-on-rails - Rails 3、Authlogic、NGINX 和 HTTP 基本身份验证不能很好地协同工作

ruby-on-rails - 为什么 "new"方法保存我的关系而不是仅仅初始化它

ruby-on-rails - 为什么 Rails 隐藏了 id 列的存在?