Posted By

andyh on 09/09/06


rails ruby pattern

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


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 =
  7. @dependent =
  8. @dependent.record = @record
  9. end
  10. if
  11. @record.attributes = params[:record]
  12. @dependent.attributes = params[:dependent]
  13. begin
  14. @record.transaction(@record, @dependent) do
  16. @record.reload unless
  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  


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 = end if @attendance.attributes = params[:record] begin @attendance.transaction(@attendance) do @attendance.reload unless 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.