How does Rails handle two people trying to edit the same record at the same time? Thanks! Regards, JJ
If you add a column called "lock_version" as an integer with a default value of zero to a table, ActiveRecord will do optimistic locking, and raise an error if the lock value has been incremented since the record was read and round-tripped to the client. If an error is raised, the record can be read from the db, and the client''s form redrawn with an error message, allowing them to reenter their change. Pretty much automatic! Dave On 2/22/05 6:52 PM, "John Johnson" <johnatl-ee4meeAH724@public.gmane.org> wrote:> How does Rails handle two people trying to edit the same record at the > same time? > > Thanks! > > Regards, > JJ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Tue, 22 Feb 2005 18:58:23 -0700, Dave Ringoen <email-aMBdsrFCgW+KfZpkr/XK+Q@public.gmane.org> wrote:> If you add a column called "lock_version" as an integer with a default value > of zero to a table, ActiveRecord will do optimistic locking, and raise an > error if the lock value has been incremented since the record was read and > round-tripped to the client. If an error is raised, the record can be read > from the db, and the client''s form redrawn with an error message, allowing > them to reenter their change. > > Pretty much automatic!Just remember to include lock_version as a hidden field on the form. My coding is not quite *that* magic ;)> Dave > > > On 2/22/05 6:52 PM, "John Johnson" <johnatl-ee4meeAH724@public.gmane.org> wrote: > > > How does Rails handle two people trying to edit the same record at the > > same time? > > > > Thanks! > > > > Regards, > > JJ > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
On 22-Feb-2005, at 22:34, Michael Koziarski wrote:> On Tue, 22 Feb 2005 18:58:23 -0700, Dave Ringoen > <email-aMBdsrFCgW+KfZpkr/XK+Q@public.gmane.org> wrote: >> If you add a column called "lock_version" as an integer with a >> default value >> Pretty much automatic! > > Just remember to include lock_version as a hidden field on the form. > My coding is not quite *that* magic ;) >>> How does Rails handle two people trying to edit the same record at >>> the >>> same time? >>>Fantastic! Thanks! I haven''t throughly tested it, but I added this to my application.rhtml, just to add a tiny bit more magic: ... <% con_sing = controller.controller_name.singularize if controller.action_name == "edit" && eval("@" + con_sing).attribute_present?("lock_version") && @content_for_layout =~ /<form.*?>/ lock_value = eval("@" + con_sing)["lock_version"].to_s lock_field = "\n<input type=''hidden''" lock_field << " name=''" + con_sing + "[lock_version]''" lock_field << " value=''" + lock_value + "''>\n" @content_for_layout = $` + $& + lock_field + $'' end %> <%= @content_for_layout %> </div> </body> </html> Now to find out where to flag ''lock_version'' as a non-display field like *_id. Thanks again! Regards, JJ
On 23-Feb-2005, at 12:43, John Johnson wrote:> On 22-Feb-2005, at 22:34, Michael Koziarski wrote: >> On Tue, 22 Feb 2005 18:58:23 -0700, Dave Ringoen >> <email-aMBdsrFCgW+KfZpkr/XK+Q@public.gmane.org> wrote: >>> If you add a column called "lock_version" as an integer with a >>> default value >>> Pretty much automatic! >> >> Just remember to include lock_version as a hidden field on the form. >> My coding is not quite *that* magic ;) >>>> How does Rails handle two people trying to edit the same record at >>>> the >>>> same time? >>>> > I haven''t throughly tested it, but I added this to my > application.rhtml, just to add a tiny bit more magic: > > Now to find out where to flag ''lock_version'' as a non-display field > like *_id. >Here''s a patch, if anyone is interested: Index: activerecord-1.6.0/lib/active_record/base.rb ==================================================================--- activerecord-1.6.0/lib/active_record/base.rb (revision 30) +++ activerecord-1.6.0/lib/active_record/base.rb (working copy) @@ -544,9 +544,10 @@ end # Returns an array of columns objects where the primary id, all columns ending in "_id" or "_count", + # columns named lock_version, # and columns used for single table inheritance has been removed. def content_columns - @content_columns ||= columns.reject { |c| c.name == primary_key || c.name =~ /(_id|_count)$/ || c.name == inheritance_column } + @content_columns ||= columns.reject { |c| c.name == primary_key || c.name =~ /(_id|_count|lock_version)$/ || c.name == inheritance_column } end # Returns a hash of all the methods added to query each of the columns in the table with the name of the method as the key Regards, JJ