/ / रेल 3 + लेनदेन: वे कैसे काम करते हैं? - माणिक, रूबी-ऑन-रेल्स -3, लेनदेन

रेल 3 + लेनदेन: वे कैसे काम करते हैं? - रूबी, रूबी-ऑन-रेल -3, लेनदेन

मेरे पास यह कोड है, जैसा कि आप देख सकते हैं कि मेरे पास दो पारम हैं। और मैं एक तालिका में नए फ़ील्ड बनाता हूं, दूसरे तालिका में मैंने अपडेट किए गए फ़ील्ड।

मुझे उन्हें MySQL में पेस्ट करना होगा। सब कुछ शांत है, लेकिन मेरे पास एक सवाल है: लेन-देन कैसे करें क्योंकि मैं डेटाबेस में पेस्ट नहीं करना चाहता हूं उनमें से केवल एक (अपडेट या बनाएं)। क्योंकि अब, अगर मैंने एक पैरा भरा है, तो तालिका ओटी अपडेट की गई है। , लेकिन मैं उन सभी को ONCE में डेटाबेस में रखना चाहता हूं। तो मेरा सवाल है ... उनमें से एक को चिपकाने से बचने के लिए लेनदेन कैसे करें?

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

और लॉग में यह त्रुटि है: (यदि मैं ग्रिड_परम को खाली छोड़ देता हूं)

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/ruby-1.9.3-p0@rails314/gems/actionpack-3.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.7ms)
Rendered /home/parallels/.rvm/gems/ruby-1.9.3-p0@rails314/gems/actionpack-3.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /home/parallels/.rvm/gems/ruby-1.9.3-p0@rails314/gems/actionpack-3.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (2.8ms)

उत्तर:

जवाब के लिए 0 № 1
ActiveRecord::Base.transaction do
...
end

एक लेनदेन में ब्लॉक लपेटेंगे। यदि आपके कुछ मॉडलों में अलग-अलग डेटाबेस कनेक्शन हैं, तो लेनदेन के लिए उस विशिष्ट वर्ग पर सही कनेक्शन पर कॉल करें।