English 中文(简体)
我如何在活跃的Record交易中拯救(没有挖掘点) se?
原标题:How do I get save (no exclamation point) semantics in an ActiveRecord transaction?

我有两个模式:PersonAddress,我想在一次交易中产生。 这就是说,我要尝试创建<代码>Person/code>,如果成功,则建立相关的<代码>Address/code>。 我愿使用<代码>save semantics (return truefalse,而不是save! semantics (raise ancode>ActiveRecord:StatementInvalid)。

这是因为<编码>用户>save。 does 引发交易退缩:

class Person
  def save_with_address(address_options = {})
    transaction do
      self.save
      address = Address.build(address_options)
      address.person = self
      address.save
    end
  end
end

(保留<代码>本身。 自助电话:栏外,其余部分则无助,因为<代码>Person即使在<代码>(<<><>><>>>>> 代码>失效时仍可节省费用。

而这种工作是因为它提出了<代码>。 积极记录:在<条码>中声明无效的例外,不触发<条码>。 积极记录:Rollback:

class Person
  def save_with_address(address_options = {})
    transaction do
      save!
      address = Address.build(address_options)
      address.person = self
      address.save!
    end
  end
end

铁路文件 具体警告不要捕获<代码> 积极记录:声明 页: 1

我猜想我的第一个问题是:为什么这种交易会阻碍......在两种储蓄中进行交易?

最佳回答

如何做到这一点?

class Person
  def save_with_address(address_options = {})
    tx_error = false
    transaction do
      begin
        self.save!
        address = Address.build(address_options)
        address.person = self
        address.save!
      rescue Exception => e
        # add relevant error message to self using errors.add_to_base
        raise ActiveRecord::Rollback 
        tx_error = true 
      end
    end
    return true unless tx_error

    # now roll back the Person TX.
    raise ActiveRecord::Rollback
    return false
  end
end

我不喜欢执行该高专。 但是,这就是你如何能够围绕这些问题开展工作。

问题回答

恳请你这样做。 避免出现问题:

class Person < ActiveRecord::Base
  belongs_to :address, :autosave => true
end

唯一一点是,个人的错误将包含正确格式的验证错误。

AutosaveAssocation。 更多信息模块。





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签