0
私はこのコードを持っていますが、私は2つのパラメータがあることがわかります。そして私は1つのテーブルで私は新しいフィールドを作成し、別のテーブルではフィールドを更新しました。Rails 3 + Transactions:どのように機能するのですか?
私はそれらをMySQLに貼り付ける必要があります。すべてがクールですが、私は質問があります。データベースを1つだけ(更新または作成)貼り付けたくないためにトランザクションを実行する方法です。今、1つのparamsを埋めれば、テーブルは更新されて作成されますが、私はそれらをすべてONEにデータベースに入れたいと思います。だから私の質問は...どのようにそれらの1つを貼り付けることを避けるためにトランザクションを行うのですか?
json_grid_params = ActiveSupport::JSON.decode(params[:grid_json])
json_form_params = ActiveSupport::JSON.decode(params[:form_json])
json_grid_params.each do |json_grid_params|
report = Report.find(:all, :conditions => ["wat_id in (?)", json_grid_params["wat_id"].to_i])
report.each do |r|
rr = r.update_attributes(:percent_money => json_grid_params["percent_money"],
:percent_item => json_grid_params["percent_item"],
:trend => json_grid_params["trend"])
end
form = FormAnswer.create(json_form_params)
は更新:
ActiveRecord::Base.transaction do
json_grid_params = ActiveSupport::JSON.decode(params[:grid_json])
json_form_params = ActiveSupport::JSON.decode(params[:form_json])
json_grid_params.each do |json_grid_params|
report = Report.find(:all, :conditions => ["wat_id in (?)", json_grid_params["wat_id"].to_i])
report.each do |r|
rr = r.update_attributes(:percent_money => json_grid_params["percent_money"],
:percent_item => json_grid_params["percent_item"],
:trend => json_grid_params["trend"])
rr.save!
form = FormAnswer.create(json_form_params)
#form.save!
end
end
end
とログにはこのエラーがあります(私は空grid_paramsを残す場合)
NoMethodError (undefined method `save!' for true:TrueClass):
app/components/report_grid.rb:122:in `block (4 levels) in <class:ReportGrid>'
app/components/report_grid.rb:118:in `each'
app/components/report_grid.rb:118:in `block (3 levels) in <class:ReportGrid>'
app/components/report_grid.rb:116:in `each'
app/components/report_grid.rb:116:in `block (2 levels) in <class:ReportGrid>'
app/components/report_grid.rb:113:in `block in <class:ReportGrid>'
Rendered /home/parallels/.rvm/gems/[email protected]/gems/actionpack-3.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.7ms)
Rendered /home/parallels/.rvm/gems/[email protected]/gems/actionpack-3.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /home/parallels/.rvm/gems/[email protected]/gems/actionpack-3.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (2.8ms)
更新:私のコードを見て、私は更新しました。 –
これは私の場合はうまくいきません:( –
はすぐに保存され、trueを返します。本当に有効にしてください。これは無効です。http://apidock.com/rails/ActiveRecord/を参照してください。 Base/update_attributes – DGM