- create
- create!
- save
- save!
- update
- update_attributes
- update_attributes!
以下方法则会跳过验证,将数据保存到数据库中
- decrement!
- decrement_counter
- increment!
- increment_counter
- toggle!
- update_all
- update_attribute
- update_counters
当使用
- save(:validate => false)
验证也会被跳过。
- validates_acceptance_of
- class Library < ActiveRecord::Base
- has_many :books
- validates_associated :books
- end
- validates_confirmation_of :password(多用于验证两次密码)
- class Account < ActiveRecord::Base validates_exclusion_of :subdomain, :in => %w(www), :message => "Subdomain %{value} is reserved."
- end (用于验证是否包含此www)
- class Product < ActiveRecord::Base validates_format_of :legacy_code, :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"end
- (格式验证)
上传时验证文件类型:
- # 验证文件后缀
- validates_format_of :photo, :with => %r{\.(gif|png|jpg)$}i, :message => "must be a URL for a GIF, JPG, or PNG image"
- # 如果使用file_column,则可以用以下方法
- validates_file_format_of :photo, :in => ["gif", "png", "jpg"]
最后我是用p.save false来实现跳过验证的
-
===
-
abstract_class?
-
accessible_attributes
-
active_connection_name (<= v2.1.0)
-
add_conditions!
-
add_group!
-
add_joins!
-
add_limit!
-
add_lock!
-
add_order!
-
aggregate_mapping
-
all
-
all_attributes_exists?
-
allow_concurrency
-
allow_concurrency=
-
arel_engine (>= v3.0.0)
-
arel_table (>= v3.0.0)
-
array_of_strings?
-
attr_accessible
-
attribute_condition
-
attribute_method? (>= v3.0.0)
-
attributes_protected_by_def...
(>= v3.0.0)
-
attr_protected
-
attr_readonly
-
base_class
-
benchmark
-
class_name
-
class_name_of_active_record...
-
class_of_active_record_desc...
-
clear_active_connection_name (<= v2.1.0)
-
clear_active_connections! (<= v2.1.0)
-
clear_all_cached_connections!
(<= v2.1.0)
-
clear_cache!
(<= v2.1.0)
-
clear_connection_cache! (<= v1.0.0)
-
clear_reloadable_connections! (<= v2.1.0)
-
colorize_logging (>= v3.0.0)
-
column_methods_hash
-
column_names
-
columns
-
columns_hash
-
compute_table_name
(>= v3.0.0)
-
compute_type
-
connected?
-
connection
-
connection= (<= v2.1.0)
-
connection_pool
-
construct_attributes_from_a...
-
construct_conditions_from_a...
(<= v1.1.6)
-
construct_finder_arel
(>= v3.0.0)
-
construct_finder_sql
-
content_columns
-
count (<= v1.0.0)
-
count_by_sql
-
create
-
current_scoped_methods
-
decrement_counter
-
default_scope
-
default_select
-
define_attr_method
-
delete
-
delete_all
-
descends_from_active_record?
-
destroy
-
destroy_all
-
determine_deprecated_finder
(<= v1.2.6)
-
determine_finder
(<= v2.1.0)
-
determine_instantiator
(<= v2.1.0)
-
encode_quoted_value
-
establish_connection
-
exists?
-
expand_attribute_names_for_...
-
expand_hash_conditions_for_...
-
expand_id_conditions
-
expand_range_bind_variables
-
extract_attribute_names_fro...
(<= v2.1.0)
-
extract_options_from_args!
(<= v1.0.0)
-
find
-
find_by_sql
-
finder_needs_type_condition?
-
find_every
-
find_from_ids
-
find_initial
-
find_last
-
find_one
-
find_some
-
find_sti_class
(>= v3.0.0)
-
first
-
full_table_name_prefix
-
get_primary_key
-
human_attribute_name
-
human_name
-
i18n_scope (>= v3.0.0)
-
increment_counter
-
inheritance_column
-
inherited
-
inspect
-
instantiate
-
last
-
log_connections (<= v2.1.0)
-
lookup_ancestors (>= v3.0.0)
-
matches_dynamic_finder?
(<= v2.1.0)
-
matches_dynamic_finder_with...
(<= v2.1.0)
-
merge_conditions
-
merge_includes
-
merge_joins
-
method_missing
-
mysql_connection
-
new
-
parse_config!
(<= v1.2.6)
-
parse_sqlite_config!
-
postgresql_connection
-
primary_key
-
protected_attributes
-
quote_bound_value
-
quoted_table_name
-
quote_value
-
raise_if_bind_arity_mismatch
-
read_methods (<= v1.0.0)
-
readonly_attributes
-
relation
(>= v3.0.0)
-
remove_connection
-
remove_stale_cached_threads!
(<= v2.1.0)
-
replace_bind_variables
-
replace_named_bind_variables
-
require_mysql (<= v2.1.0)
-
reset_column_information
-
reset_column_information_an...
-
reset_counters
-
reset_primary_key
-
reset_sequence_name
-
reset_subclasses
-
reset_table_name
-
respond_to?
-
retrieve_connection
-
reverse_sql_order
-
safe_to_array
-
sanitize
-
sanitize_sql
(<= v1.2.6)
-
sanitize_sql_array
-
sanitize_sql_for_assignment
-
sanitize_sql_for_conditions
-
sanitize_sql_hash
(<= v1.2.6)
-
sanitize_sql_hash_for_assig...
-
sanitize_sql_hash_for_condi...
-
scope
-
scoped?
-
scoped_methods
-
scoped_methods=
(<= v1.0.0)
-
self_and_descendants_from_a...
-
self_and_descendents_from_a... (<= v2.2.1)
-
sequence_name
-
serialize
-
serialized_attributes
-
set_inheritance_column
-
set_locking_column (<= v1.1.6)
-
set_primary_key
-
set_readonly_option!
-
set_sequence_name
-
set_table_name
-
silence
-
single_threaded_active_conn... (<= v2.1.0)
-
single_threaded_scoped_methods
(<= v2.1.0)
-
sqlite3_connection
-
sqlite_connection
-
sti_name
-
subclasses
-
table_exists?
-
table_name
-
threaded_connections (<= v1.0.0)
-
threaded_connections= (<= v1.0.0)
-
thread_safe_active_connections (<= v2.1.0)
-
thread_safe_scoped_methods
(<= v2.1.0)
-
type_condition
-
type_name_with_module
-
undecorated_table_name
-
unscoped (>= v3.0.0)
-
update
-
update_all
-
update_counters
-
validate_find_options
-
verification_timeout
-
verification_timeout=
-
verify_active_connections! (<= v2.1.0)
-
with_exclusive_scope
-
with_scope
-
==
-
[]
-
[]=
-
arel_attributes_values
(>= v3.0.0)
-
assign_attributes
-
assign_multiparameter_attri...
-
attribute_for_inspect
-
attribute_names
-
attribute_present?
-
attributes
-
attributes=
-
attributes_before_type_cast
-
attributes_from_column_defi...
-
attributes_protected_by_def...
-
attributes_with_quotes
-
becomes
-
cache_key
-
clone
-
clone_attributes
-
clone_attribute_value
-
column_for_attribute
-
comma_pair_list
-
connection
-
convert_number_column_value
-
create
-
create_or_update
-
decrement
-
decrement!
-
define_question_method
(<= v1.2.6)
-
define_read_method
(<= v1.2.6)
-
define_read_method_for_seri...
(<= v1.2.6)
-
define_read_methods
(<= v1.2.6)
-
delete
-
destroy
-
destroyed?
-
dup (>= v3.0.0)
-
ensure_proper_type
-
eql?
-
evaluate_read_method
(<= v1.2.6)
-
execute_callstack_for_multi...
-
extract_callstack_for_multi...
-
find_parameter_position
-
freeze
-
frozen?
-
has_attribute?
-
hash
-
id
-
id=
-
id_before_type_cast
-
increment
-
increment!
-
initialize_copy (>= v3.0.0)
-
inspect
-
instantiate_time_object
-
interpolate_sql
-
log_protected_attribute_rem...
-
method_missing
(<= v1.2.6)
-
new_record?
-
object_from_yaml
-
persisted?
-
query_attribute
(<= v1.2.6)
-
quote
(<= v1.2.6)
-
quote_columns
-
quoted_column_names
-
quoted_comma_pair_list
-
quoted_id
-
quote_value
-
read_attribute
(<= v1.2.6)
-
read_attribute_before_type_...
(<= v1.2.6)
-
readonly!
-
readonly?
-
reload
-
remove_attributes_protected...
-
remove_readonly_attributes
-
respond_to? (<= v1.2.6)
-
save
-
save!
-
toggle
-
toggle!
-
to_param
-
to_xml (<= v1.1.6)
-
type_cast_attribute_value
-
unserializable_attribute?
(<= v1.2.6)
-
unserialize_attribute
(<= v1.2.6)
-
update
-
update_attribute
-
update_attributes
-
update_attributes!
-
write_attribute
(<= v1.2.6)
= private
= protected
update_attributes(attributes) publicUpdates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.
Only attr_accessible attributes will be updated
If your model specified attr_accessible attributes, only those attributes will be updated.
Use attr_accessible to prevent mass assignment (by users) of attributes that should not be editable by a user. Mass assignment is used in create and update methods of your standard controller.
For a normal user account, for example, you only want login and password to be editable by a user. It should not be possible to change the status attribute through mass assignment.
class User < ActiveRecord::Base attr_accessible :login, :password end
So, doing the following will merrily return true, but will not update the status attribute.
@user.update_attributes(:status => 'active')
If you want to update the status attribute, you should assign it separately.
@user.status = 'active' save
Calls attribute setter for each key/value in the hash
This is a convenience to set multiple attributes at the same time. It calls the "setter" method
self.attribute=(value)
for each key in the hash. If you have overridden the setter to add functionality, it will be called.
This also allows you to create non-table attributes that affect the record. For instance, a full_name=() method could parse the string and set the first_name=() and last_name() accordingly.
Skipping validation
Unlike the save method, you can’t pass false to update_attributes to tell it to skip validation. Should you wish to do this (consider carefully if this is wise) update the attributes explicitly then call save and pass false:
@model_name.attributes = params[:model_name] @model_name.save false