Posted By

andyh on 09/09/06


Tagged

rails ruby pattern


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

padraic
webstic


Multiple Record Save Action Pattern


 / Published in: Ruby
 

  1. def multiple_record_action
  2. if params[:id]
  3. @record = CustomRecord.find(params[:id])
  4. @dependent = @record.dependent
  5. else
  6. @record = CustomRecord.new
  7. @dependent = Dependent.new
  8. @dependent.record = @record
  9. end
  10. if request.post?
  11. @record.attributes = params[:record]
  12. @dependent.attributes = params[:dependent]
  13. begin
  14. @record.transaction(@record, @dependent) do
  15. @record.save
  16. @record.reload unless @record.id
  17. @dependent.save
  18. raise ActiveRecord::RecordInvalid unless @record.valid? && @dependent.valid?
  19. redirect_to :action=>'some_other_action'
  20. end
  21. rescue ActiveRecord::RecordInvalid
  22. end
  23. end
  24. end

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: veil on April 18, 2008

hi, im a newbie in ruby on rails and i have some difficulties to understand the code. i tried your code but it was not successful. hope you can see and give me some ideas on this. thank you. by the way, i need your response as soon as possible.

def createattendance if params[:id] @attendance = Teachersubject.find(params[:id]) else @attendance = Attendance.new end if request.post? @attendance.attributes = params[:record] begin @attendance.transaction(@attendance) do @attendance.save @attendance.reload unless @attendance.id raise ActiveRecord::RecordInvalid unless @attendance.valid? redirect_to :action=>'eattendance' end rescue ActiveRecord::RecordInvalid end end end

You need to login to post a comment.