- 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来实现跳过验证的
update_attributes

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