ActiveRecord::Base
save, save!, create, create!, update_attribute, update_attributes, update, update_all, delete, delete_all, destroy, destroy_all
1. save vs save!
save 成功 => true
save 失敗 => fasle
另外 save(perform_validation = true), save 還可以丟個參數進去, save(false) 的話, 一堆validate 都不跑, 可是會跑before_*, 失敗一樣是return false
save! 成功 => true
save! 失敗 => validate 失敗:RecordInvalid execption,
before_* 失敗 : RecordNotSaved
2. create vs create!
create 成功 => ActiveRecord object
create 失敗 => ActiveRecord object, 所以要確認成功失敗可以用 errors.empty? 檢查
create! 成功 => ActiveRecord object
create! 失敗 => RecordInvalid
3. 如果是資料庫的validate 就會爆 StatementInvalid
例如有這樣的設定 t.column :user_id, :integer, :null => false
沒填user_id 就會爆StatementInvalid
4. update_attribute vs update_attributes
除了可以更新update的多寡之外,
還有 update_attribute 是 save(false), 它不跑 validate
同樣成功true, 失敗false
5 update, update_all
丟的參數差很多
update(id, attributes)
update_all(updates, conditions = nil, options = {})
6.delete_all, delete, destroy, destroy_all
delete 是不會跑before_, after_ destroy 還有 dependent 也是不跑的
destroy_all(conditions = nil)
delete_all(conditions = nil)
7. ConnectionAdapters::DatabaseStatements
connection.select_all
8. ActiveRecord::ConnectionAdapters::AbstractAdapter
ActiveRecord::Base.connection returns an AbstractAdapter object,
2009年2月5日 星期四
2008年11月18日 星期二
validate,before,create,define_method,callback
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html#M001618
http://www.ruby-doc.org/core/classes/Module.html#M001682
* (-) save
* (-) valid
* (1) before_validation
* (2) before_validation_on_create
* (-) validate
* (-) validate_on_create
* (3) after_validation
* (4) after_validation_on_create
* (5) before_save
* (6) before_create
* (-) create
* (7) after_create
* (8) after_save
define_method: 動態產生method 的好物
http://www.ruby-doc.org/core/classes/Module.html#M001682
* (-) save
* (-) valid
* (1) before_validation
* (2) before_validation_on_create
* (-) validate
* (-) validate_on_create
* (3) after_validation
* (4) after_validation_on_create
* (5) before_save
* (6) before_create
* (-) create
* (7) after_create
* (8) after_save
define_method: 動態產生method 的好物
2008年7月17日 星期四
create table, column
def self.up
create_table :mugshots do |t|
t.column :user_id, :integer
t.column :created_at, :datetime
t.column :parent_id, :integer
t.column :content_type, :string
t.column :filename, :string
t.column :thumbnail, :string
t.column :size, :integer
t.column :width, :integer
t.column :height, :integer
end
end
CREATE TABLE `mugshots` (`id` int(11) DEFAULT NULL auto_increment
PRIMARY KEY, `user_id` int(11) DEFAULT NULL NULL, `created_at`
datetime DEFAULT NULL NULL, `parent_id` int(11) DEFAULT NULL NULL,
`content_type` varchar(255) DEFAULT NULL NULL, `filename`
varchar(255) DEFAULT NULL NULL, `thumbnail` varchar(255) DEFAULT
NULL NULL, `size` int(11) DEFAULT NULL NULL, `width` int(11) DEFAULT
NULL NULL, `height` int(11) DEFAULT NULL NULL) ENGINE=InnoDB
訂閱:
文章 (Atom)