Find的參數
1.select
2.group
統計的method
有average, maximum, minimum, sum, count
1. count, group
2. maximum
memo: find_by_sql, count_by_sql
find_or_initialize_by, find_or_create_by
2009年2月5日 星期四
2008年11月11日 星期二
strain, 寫成這樣好醜 XD
if params[:type]
SmsRecord.sys
else
SmsRecord.purchase
end.find(:all, :conditions => cond).each do |row|
csv << [row.id, row.sms_purchase_record_id, row.user_id, row.send_to_sms_at.strftime('%Y/%m/%d %H:%M'), row.sms_rsp_code]
end
2008年9月4日 星期四
jquery each find what is different
[html]
<div id="'test'">
<span id="'each'">(click here to change)</span>
<ul>
<li class="cc">Eat</li>
<li class="cc">Sleep</li>
<li class="bb">Be merry</li>
</ul>
</div>
[script 1]
$("#test span").click(function () {
if ($("#test").find('li')) {
alert($(this).html());
}
});
$(this) is $(#test span)
[script 2]
$("#test span").click(function () {
if ($("#test li")) {
alert($(this).html());
}
});
$(this) is $(#test span)
[script 3]
$("#test span").click(function () {
$("#test li").each(function(i, e){
alert($(this).html());
}
});
$(this) is really every li tag
i is the iterator counter
e == $(this)
Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).
2008年8月21日 星期四
filter, find
http://manalang.com/jquery/docs/index.html#filter-expr
http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links
http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links
$(document).ready(function() {
$('#extlinks a').filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).after(' <img src="/images/external.png" alt="external link">');
});
filter 可以塞function來parse, return true 才會跑後面的after
2008年8月12日 星期二
jQuery tutorial, find and filter
http://www.learningjquery.com/2006/11/how-to-get-anything-you-want-part-1
http://www.learningjquery.com/2006/12/how-to-get-anything-you-want-part-2
http://www.learningjquery.com/2006/12/how-to-get-anything-you-want-part-2
filter will select a certain subset (zero or more) of the already
selected elements.find will select a set of (zero or more) elements that are descendants
of the already selected elements.Here is an example:
$('div').filter('.elephants'); // <-- selects the second div, because it has class="elephants"$('div').find('.elephants'); // <-- selects the first paragraph, because it has class="elephants"
* Note that these two examples are very simple and would probably be
better written as ...$('div.elephants');
... and ...
$('div .elephants');$('li + li > a[@href$=pdf]')gets all links ending in
“pdf” that are children of any list item that has another list item as
its previous sibling. It won’t get the first list item’s silly.pdf
because that list item has no other list items before it.$('span:hidden')gets any span element that is hidden.$('li:even')gets all odd-numbered list items (because, in javascript, numbering starts with 0, not 1).$('li:lt(3)')gets the first 3 list items. “lt” stands for “less than,” and it starts counting at 0, not 1.$('li:not(.goofy)')gets list items 1, 2, and 4, because they’re not “goofy.”$('p a[@href*=#]')gets any links that are inside a paragraph and have an “href” attributestarting with “#” —containing “#” anywhere.in other words, same-page links. There are problems with trying to identify same-page links this way. I’ll write about this in an upcoming entry. Note the space between the “p” and the “a”; that means that the “a” is a descendant of the “p.”
2008年7月20日 星期日
find random offset
http://blog.pbg4.org/2008/1/2/find-randomly-redux
http://wiki.rubyonrails.org/rails/pages/HowtoSelectRandomRecords
http://groups.google.com/group/Ruby-on-Rails-Taiwan/browse_thread/thread/27ec511fa258ca08
http://blog.commonthread.com/2007/12/7/tip-ruby-on-rails-random-array-element
http://wiki.rubyonrails.org/rails/pages/HowtoSelectRandomRecords
http://groups.google.com/group/Ruby-on-Rails-Taiwan/browse_thread/thread/27ec511fa258ca08
http://blog.commonthread.com/2007/12/7/tip-ruby-on-rails-random-array-element
目前一般的最佳解是這個, random 出offestps:console下def self.find(*args)
scope = args.first
if scope.to_s == "random"
super :first, :offset => (rand count).to_i
elsif scope.to_s == "public"
# blah, blah...
else
super
end
end
Record.find(:first, :offset => (rand Record.count).to_i)
要用Record.count, sometimes some record would be deleted
ps2:bad way find(rand(count) + 1) rescue ActiveRecord::RecordNotFound random
2008年6月9日 星期一
訂閱:
文章 (Atom)