method
toggle
rails latest stable - Class:
ActiveRecord::Base
Method deprecated or moved
This method is deprecated or moved on the latest stable version. The last existing version (v2.3.8) is shown here.
toggle(attribute)public
Assigns to attribute the boolean opposite of attribute?. So if the predicate returns true the attribute will become false. This method toggles directly the underlying value without calling any setter. Returns self.
# File activerecord/lib/active_record/base.rb, line 2714
def toggle(attribute)
self[attribute] = !send("#{attribute}?")
self
end 2Notes
Skips validations and callbacks
tobago · Mar 11, 20101 thankThe method skips validations and callbacks. That is why it should be used with caution.
==== Code example
person.toggle :active
Takes attribute as a symbol
insane-dreamer · Apr 11, 2009Attribute must be passed as a symbol:
User.toggle(:funny)
not
User.toggle(funny)
Related methods
- Instance methods
- ==
- []
- []=
- attribute_for_inspect
- attribute_names
- attribute_present?
- attributes
- attributes=
- attributes_before_type_cast
- becomes
- cache_key
- clone
- column_for_attribute
- connection
- decrement
- decrement!
- delete
- destroy
- destroyed?
- eql?
- freeze
- frozen?
- has_attribute?
- hash
- id
- id=
- id_before_type_cast
- increment
- increment!
- inspect
- new_record?
- quoted_id
- readonly!
- readonly?
- reload
- save
- save!
- to_param
- toggle
- toggle!
- update_attribute
- update_attributes
- update_attributes!
- Class methods
- ===
- abstract_class?
- accessible_attributes
- all
- allow_concurrency
- allow_concurrency=
- attr_accessible
- attr_protected
- attr_readonly
- base_class
- benchmark
- class_name
- column_methods_hash
- column_names
- columns
- columns_hash
- connected?
- connection
- connection_pool
- content_columns
- count_by_sql
- create
- decrement_counter
- delete
- delete_all
- descends_from_active_record?
- destroy
- destroy_all
- establish_connection
- exists?
- find
- find_by_sql
- finder_needs_type_condition?
- first
- full_table_name_prefix
- get_primary_key
- human_attribute_name
- human_name
- increment_counter
- inheritance_column
- inherited
- inspect
- last
- merge_conditions
- mysql_connection
- new
- postgresql_connection
- primary_key
- protected_attributes
- quote_value
- readonly_attributes
- remove_connection
- reset_column_information
- reset_column_information_and_inheritable_attributes_for_all_subclasses
- reset_counters
- reset_primary_key
- reset_sequence_name
- reset_subclasses
- reset_table_name
- respond_to?
- retrieve_connection
- sanitize
- self_and_descendants_from_active_record
- sequence_name
- serialize
- serialized_attributes
- set_inheritance_column
- set_primary_key
- set_sequence_name
- set_table_name
- silence
- sqlite3_connection
- sqlite_connection
- sti_name
- table_exists?
- table_name
- update
- update_all
- update_counters
- verification_timeout
- verification_timeout=
- Protected methods
-
aggregate_mapping -
class_name_of_active_record_descendant -
class_of_active_record_descendant -
compute_type -
current_scoped_methods -
default_scope -
encode_quoted_value -
expand_hash_conditions_for_aggregates -
expand_range_bind_variables -
quote_bound_value -
raise_if_bind_arity_mismatch -
replace_bind_variables -
replace_named_bind_variables -
sanitize_sql_array -
sanitize_sql_for_assignment -
sanitize_sql_for_conditions -
sanitize_sql_hash_for_assignment -
sanitize_sql_hash_for_conditions -
scope -
scoped? -
scoped_methods -
set_readonly_option! -
subclasses -
validate_find_options -
with_exclusive_scope -
with_scope - Private methods
-
add_conditions! -
add_group! -
add_joins! -
add_limit! -
add_lock! -
add_order! -
all_attributes_exists? -
array_of_strings? -
attribute_condition -
construct_attributes_from_arguments -
construct_finder_sql -
default_select -
define_attr_method -
expand_attribute_names_for_aggregates -
expand_id_conditions -
find_every -
find_from_ids -
find_initial -
find_last -
find_one -
find_some -
instantiate -
merge_includes -
merge_joins -
method_missing -
parse_sqlite_config! -
quoted_table_name -
reverse_sql_order -
safe_to_array -
type_condition -
type_name_with_module -
undecorated_table_name -
assign_attributes -
assign_multiparameter_attributes -
attributes_from_column_definition -
attributes_protected_by_default -
attributes_with_quotes -
clone_attribute_value -
clone_attributes -
comma_pair_list -
convert_number_column_value -
create -
create_or_update -
ensure_proper_type -
execute_callstack_for_multiparameter_attributes -
extract_callstack_for_multiparameter_attributes -
find_parameter_position -
instantiate_time_object -
interpolate_sql -
log_protected_attribute_removal -
object_from_yaml -
quote_columns -
quote_value -
quoted_column_names -
quoted_comma_pair_list -
remove_attributes_protected_from_mass_assignment -
remove_readonly_attributes -
type_cast_attribute_value -
update