# Double data entry

**URL:** <https://www.ruby-forum.com/t/double-data-entry/72319>\
**Category:** Rails\
**Created:** [October 16, 2006, 12:39pm UTC](https://www.ruby-forum.com/t/double-data-entry/72319 "2006-10-16T12:39:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 16, 2006, 12:39pm UTC](https://www.ruby-forum.com/t/double-data-entry/72319/1 "2006-10-16T12:39:21Z")

</div>

I’m developing database systems for clinical trials using Ruby on Rails.  
Some trials require ‘double data entry’ as a form of validation; I need  
to incorporate this into my system and am having difficulties. My plan  
would be to have it work as follows:

- Once a new record has been created, alongside the usual ‘edit’ option  
there would be a ‘second data entry’ option.
- On clicking this, a ‘blank’ record would appear (as if a new record) -  
but when submitted, this form data would be compared with the existing  
record on the database.
- If any differences were found this form would be displayed again with  
the relevant fields highlighted (Ã la validation, with the accompanying  
text showing the previous values) and the user asked to confirm.
- After confirmation (or if the second input exactly matched the first  
initially) the reocrd would be saved and a flag will be set in the table  
that double data entry has been performed for that record.

Hopefully this could be handled in the same (edit) method by passing a  
parameter (data\_entry\_mode or something). Ideally the second version  
would not involve creating a new record in the database; any changes  
would simply be written over the top of the first (perhaps the second  
data entry values would be stored as session variables??).

Does anyone have any ideas on how best to accomplish this?

I apologise for the long and unusual nature of this query. Thank you for  
any thoughts.

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 16, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/double-data-entry/72319/2 "2006-10-16T14:06:27Z")

</div>

On Oct 16, 6:39 am, Tim C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> - If any differences were found this form would be displayed again with  
> data entry values would be stored as session variables??).
> 
> Does anyone have any ideas on how best to accomplish this?
> 
> I apologise for the long and unusual nature of this query. Thank you for  
> any thoughts.
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Hi Tim,

First off, I have an interest in this sort of application myself.  
Please contact me off-list if you would like additional help.

The proper way to do double entry rather depends on your workflow.

- Do you have the same person enter the data twice?
- Does the second entry happen at a different time, or do you want to  
do it all in one form?

As a general rule, double entry makes for a bad user interface, but  
there are times when it is necessary (password verifications show up a  
lot).

In any event, one way to implement this would be through an ‘edit’ like  
action.

- record has been entered
- user invokes the ‘verify’ action on the record.
- ‘verify’ generates a blank form (much like a new item form) but  
passes the id of the record in a hidden field
- on submission, load the original object in memory
- cycle through all the attributes of the record looking for  
differences. Ignore at ‘created\_at’ like attributes.
- If all the relevant columns pass, then update the ‘verified\_at’ with  
the timestamp of when it passed the test.
- If not, then assign the ‘errors’ for the record and re-render the  
form with the discrepancies highlighted.

\_Kevin  
[www.sciwerks.com](http://www.sciwerks.com)

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 16, 2006, 2:54pm UTC](https://www.ruby-forum.com/t/double-data-entry/72319/3 "2006-10-16T14:54:03Z")

</div>

Hi Kevin

> First off, I have an interest in this sort of application myself.  
> Please contact me off-list if you would like additional help.

Thank you. I expect I shall do so soon!

> - Do you have the same person enter the data twice?

No.

> - Does the second entry happen at a different time, or do you want to  
> do it all in one form?

A different time. (The idea being that double data entry can be used as  
an extra for some forms in some studies - and perhaps only for a sample  
of the records, with a status flag to indicate if a given record has  
been entered twice.)

> In any event, one way to implement this would be through an ‘edit’ like  
> action.
> 
> - record has been entered
> - user invokes the ‘verify’ action on the record.
> - ‘verify’ generates a blank form (much like a new item form) but  
> passes the id of the record in a hidden field
> - on submission, load the original object in memory
> - cycle through all the attributes of the record looking for  
> differences. Ignore at ‘created\_at’ like attributes.
> - If all the relevant columns pass, then update the ‘verified\_at’ with  
> the timestamp of when it passed the test.
> - If not, then assign the ‘errors’ for the record and re-render the  
> form with the discrepancies highlighted.

This is exactly the sort of process flow that I’m after! Now I just need  
to work out the code. I shall have a go, but any help on these pages  
would be appreciated (especially as, ideally, this might end up as a  
plugin so that there wouldn’t be the need to write the same code in  
every edit method!).

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 17, 2006, 6:55am UTC](https://www.ruby-forum.com/t/double-data-entry/72319/4 "2006-10-17T06:55:07Z")

</div>

Just a thought, my acts\_as\_modified plugin may be of use to you when  
looking  
for differences between the records.

You could assign the new values to the original record then use  
record.modified\_attributes see any differences.

[http://svn.viney.net.nz/things/rails/plugins/acts\_as\_modified](http://svn.viney.net.nz/things/rails/plugins/acts_as_modified)

-Jonathan.

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 19, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/double-data-entry/72319/5 "2006-10-19T17:20:10Z")

</div>

I’ve had a go at writing a verify method…

def verify  
@subject = Subject.new  
@subject.id = params[:id]

```
if request.post?
  @subject_original = Subject.find(params[:id]) #load original 

```

record  
changes = [] #array to store changes  
@confirm = true #when form is submitted, change button to  
‘Confirm’  
@subject\_original.attributes.each do |key, value|  
if params[:subject][key]  
@transaction = record\_transaction(‘subjects’, key,  
@subject\_original.id, value, params[:subject][key], value.class.to\_s)  
changes \<\< @transaction if @transaction#write any changes  
into array  
end  
end

```
  case params['commit']
    when "Next" then
      for change in changes
        @subject.errors.add(change.field, change.old_value.to_s)
      end
    when "Confirm" then
      @subject_original.update_attributes(params[:subject])
      @subject_original.save
  end
end

```

end

This uses a method called ‘record\_transaction’ from my application  
controller to only add to the changes array if there has been a change.  
Jonathan - I’m sure that your plugin would be useful here for doing the  
comparisons and building up the changes array instead of using  
record\_transaction - can you give me any help as to how I’d fit it into  
the code above?

When the verification form is submitted, the page is currently  
redisplayed with any differences shown as if they were validation  
errors. (And the ‘next’ button changes to ‘confirm’ so that the next  
form submission writes to the DB.) Rather than validation errors, is  
there an easy way for me to write something such that I can do  
@subject.verifications.add(…), which would work a bit like the  
Automatic Rails validation, but, as well as highlighting fields with  
differences, display the old values next to each differing field?

Thanks.

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 25, 2006, 5:01pm UTC](https://www.ruby-forum.com/t/double-data-entry/72319/6 "2006-10-25T17:01:39Z")

</div>

Thanks Jonathan.

This has saved me several lines of code and works a treat! What an  
excellent plugin.

Tim

---

<div class="post-metadata">

**Author:** ![ratcheer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/aca169/32.png) [@ratcheer](https://www.ruby-forum.com/u/ratcheer)\
**Post date:** [October 25, 2006, 5:27am UTC](https://www.ruby-forum.com/t/double-data-entry/72319/7 "2006-10-25T05:27:29Z")

</div>

The docs should be enough:

[http://svn.viney.net.nz/things/rails/plugins/acts\_as\_modified/lib/acts\_as\_modified.rb](http://svn.viney.net.nz/things/rails/plugins/acts_as_modified/lib/acts_as_modified.rb)

But just quickly, you can do:

@subject = Subject.find(params[:id])  
@subject.attributes = { :name =\> “New name” }

@subject.modified\_attributes # { :name =\> “Old name” }  
@subject.name\_modified? # true  
@subject.original\_name # “Old Name”

@subject.modified? # true

Feel free to email me off-list if you want more assistance with the  
plugin.

-Jonathan.
