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年10月23日 星期四
svn, update
http://svnbook.red-bean.com/en/1.0/re28.html
You can also update your working copy to an older revision
You can also update your working copy to an older revision
svn update -r30
svn diff -rPREV
svn diff file_name -rPREV
svn log --limit
svn log --limit 2 main.css
svn cat -r5323 show.rhtml
svn merge -r7311:7389 svn://svn.icetech.com.tw/serac/youholder/branches/haruko
svn import earlybirds/ svn://bigcat.dlinkddns.com/eb/trunk
2008年9月5日 星期五
rjs update layout
http://www.codefluency.com/articles/2006/05/27/rails-views-render-update-not-rjs/
http://www.scribd.com/doc/220397/RJShow-it-works
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html
http://www.scribd.com/doc/220397/RJShow-it-works
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html
[test rjs]
page.you_holder.cde
[application.js]
YouHolder.cde = function(){
(function($){
alert('lalala');
})(jQuery);
}
layout proc{ |c| c.request.xhr? ? false : "application" }
2008年9月4日 星期四
render page update
[view]
function ajax_create(act, pos, goals) {
$.ajax({
url: '/goals/'+act,
type: 'POST',
data: goals,
dataType: 'html',
success:function(html){
$(pos).after(html);
$('td.goal_del').unbind('click', goal_del).bind('click', goal_del);
$('td.goal_up_tmp').unbind('click', goal_up ).bind('click', goal_up);
$('td.goal_down_tmp').unbind('click', goal_down).bind('click', goal_down);
}
});
}
[controller]
render :partial => 'member/temp_goal', :locals => {:g => new_goal}
[view]
<%= observe_field :target_type_target_type_id, :on => "change",
:url => {:action => :update_target_list},
:with => "{target_type_id: value, target_cat_id: $('target_type_target_cat_id').value}"%>
(用page 寫 js)
[controller]
render :update do |page|
page.replace_html 'target_type_target_id', :partial => 'target_type_select', :locals => {:types => target_list}
end
[view]
function test() {
$.ajax({
url: '/alert/test',
type: 'GET',
success : function(html){
alert(html);
}
});
}
[controller]
render :action => 'test', :layout => false
[test view]
<% response.headers['Content-Type'] = "text/javascript" %>
alert(<%=1+1 %>);
[view]
function test() {
$.ajax({
url: '/alert/test',
type: 'GET',
dataType: 'script'
});
}
[controller]
render :action => 'test', :layout => false
[test view]
<% response.headers['Content-Type'] = "text/javascript" %>
alert(<%=1+1 %>);
[view]
function test() {
$.ajax({
url: '/alert/test',
type: 'GET',
dataType: 'script'
});
}
[controller]
render :update do |page|
page.alert('cccccc') #page << "alert('cccc');" the same end
[view]
function test() {
$.ajax({
url: '/alert/test',
type: 'GET',
dataType: 'script'
});
}
[controller]
def test
#nothing
end
[test view] test.rjs
page << "alert('cccccc');"
2008年7月24日 星期四
update, replace_html
http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M001645
replace_html可以用partial 去取代
replace_html可以用partial 去取代
render :update do |page|
page.replace_html 'target_type_target_type_id',
:partial => 'target_type_select', :locals => {:types => target_type_content}
end
訂閱:
文章 (Atom)