SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Comparing form value with db value on edit

    I am creating a small job tracker application for my office. When a job is edited, I add a new record to an audit database saying "Job edited on date ". I would also like to say what was edited. How can I do a simple compare of the form value to the database value? I would assume it would be hooked into the before_update callback. Any help would be appreciated.

  2. #2
    SitePoint Enthusiast kyko's Avatar
    Join Date
    May 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think you will need a before update callback since it really isn't a hook that you need here but rather just another variable to compare against. you can do the comparison inside the update_attrubutes if statement and save the results to your log table there.
    Code:
    def update_job
      @job = Job.find(params[:id]) # this gets updated
      @old_job = Job.find(params[:id]) # this stays the same so you can compare
      if @job.update_attributes(params[:job]) # @job should now be updated
        log = ""
        for key, value in @job.attributes # goes through every attribute in @job
          unless value == @old_job[key] # checks with the old version
            log << "#{key} was changed from #{@old_job[key]} to #{value}.\n" # appends log if different
          end
          # Here you can save the changes to the DB as seperate entries
        end
        # Here you can save the log to the DB as one entry
      end
    end
    Stop Global &nbsp;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •