Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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/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
Ruby on rails rails 3中的事务_Ruby On Rails_Ruby On Rails 3_Activerecord_Transactions - Fatal编程技术网

Ruby on rails rails 3中的事务

Ruby on rails rails 3中的事务,ruby-on-rails,ruby-on-rails-3,activerecord,transactions,Ruby On Rails,Ruby On Rails 3,Activerecord,Transactions,我想在事务中添加以下代码,以便在任何插入中出现错误时能够回滚 def create m = params[:message] # EmailThread.transaction do #<=== is this correct? if m[:parent_id].nil? thread = EmailThread.new :title => m[:subject] thread.save else th

我想在事务中添加以下代码,以便在任何插入中出现错误时能够回滚

def create
    m = params[:message]

    # EmailThread.transaction do #<=== is this correct?
    if m[:parent_id].nil?
        thread = EmailThread.new :title => m[:subject]
        thread.save
    else
        thread = EmailThread.find m[:parent_id]
    end

    message = thread.messages.build :content => m[:content], :author_id => current_user.id
    message.save

    from = thread.participants.build :user_id => current_user.id
    to   = thread.participants.build :user_id => m[:to_user_id]

    from.save
    to.save

    #end  #<=== to here

    render :json => {:success=>true,:message=>"Message sent"}

end
def创建
m=参数[:消息]
#EmailThread.transaction do#m[:主题]
thread.save
其他的
thread=EmailThread.find m[:父线程id]
结束
message=thread.messages.build:content=>m[:content],:author\u id=>current\u user.id
message.save
from=thread.participants.build:user\u id=>current\u user.id
to=thread.participants.build:user\u id=>m[:to\u user\u id]
从
拯救
#结束#{:success=>true,:message=>messagesent}
结束
我读到在控制器中定义事务不是一个好的实践,有人能帮我解决这个问题吗

关于

,如中所述,没有必要将关联与其父对象分开显式保存

在您的情况下,由于在控制器中创建的所有对象都是“依赖的”,因此,首先使用build创建所有关联,然后最后保存父对象就足够了。这将自动保存所有依赖关联


只有在同一操作中创建两个或多个完全独立但有某种关联的对象时,才需要在控制器中启动事务。我能想到的最好的例子是一笔汇款,包括一笔债务和随后的两个银行账户贷记。这两个帐户都不是“连接”的,但彼此非常依赖:如果一个失败,另一个也应该失败。这时transactiond是唯一的解决方案

我认为丹尼的答案是正确的。但是,如果您需要在控制器上执行事务,那么它不是执行事务的地方。您可以在模型中创建一个方法来实现这一点

例如:

class Model < ActiveRecord::Base
  def self.some_method some_params
    Model.transaction do
      #do your stuff
      #return true if ok.
    end
  end
end
def create
  if Model.some_method #with the params you want to pass
     #render success
  else
     #render error
  end
end