Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11098 (closed defect: fixed)

Opened 8 months ago

Last modified 7 months ago

[PATCH][DOCS][TINY] ActiveRecord::Base#toggle! - bad implementation or documentation

Reported by: DefV Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: minor Keywords: patch docs tiny verified
Cc:

Description

ActiveRecord::Base#toggle! is used to toggle an attribute true or false, and immediatly save it. It is implemented to call the toggle method, and then call update_attribute to save to the database.

The problem I see with this is that there is no validation, when toggling a boolean. In my opinion this is wrong, and there should be validation. I've heard people say this behaviour was correct, and that's how they wanted it, in which case the documentation is incomplete and can lead to unexpected results.

def toggle!(attribute)
  toggle(attribute).update_attribute(attribute, self[attribute])
end

You tell me, a patch to make toggle! validate or a documentation patch to say toggle! doesn't validate when saving.

Attachments

toggle_increment_decrement_doc.diff (1.1 kB) - added by cavalle on 03/04/08 22:36:26.

Change History

02/13/08 15:05:47 changed by jamesh

This is expected behaviour. It probably wouldn't hurt to not that validation won't occur.

02/13/08 16:07:55 changed by blj

Please explain why the boolean field need a validation?

-1

02/13/08 17:03:32 changed by cpytel

+1 to change toggle to honor validations. I'd also +1 a patch to remove toggle! entirely.

02/13/08 17:05:37 changed by jamesh

If you want toggle with validations:

model.toggle(:attribute_name).save

# or

model.toggle(:attribute_name).save!

02/13/08 17:07:33 changed by lifofifo

-1 for adding validations.

+1 for any docs you would like to add anywhere :)

02/14/08 14:24:11 changed by ncr

+1 for docs, -1 for validations.

03/04/08 22:36:26 changed by cavalle

  • attachment toggle_increment_decrement_doc.diff added.

03/04/08 22:37:37 changed by cavalle

  • keywords set to patch docs tiny.
  • summary changed from ActiveRecord::Base#toggle! - bad implementation or documentation to [PATCH][DOCS][TINY] ActiveRecord::Base#toggle! - bad implementation or documentation.

doc patch attached

03/05/08 10:37:03 changed by ncr

+ 1

03/11/08 09:15:19 changed by juanjo.bazan

  • keywords changed from patch docs tiny to patch docs tiny verified.

+1 for the doc patch

03/11/08 11:34:38 changed by ernesto.jimenez

+1 for the doc patch

03/11/08 12:05:11 changed by pratik

  • status changed from new to closed.
  • resolution set to fixed.

(In [9011]) Add warning in documentation for increment!, decrement! and toggle! methods. Closes #11098 [DefV]