我寫的後續代碼從表webeehs
得到一個記錄:如何獲取表列值?
webeehs_result = Webeeh.find(:all, :conditions=>["webeeh_project_id=#{project_id}"])
然後,我想從這個紀錄一列值,我該怎麼辦?
例如,列名是webeeh_date
。
我寫的後續代碼從表webeehs
得到一個記錄:如何獲取表列值?
webeehs_result = Webeeh.find(:all, :conditions=>["webeeh_project_id=#{project_id}"])
然後,我想從這個紀錄一列值,我該怎麼辦?
例如,列名是webeeh_date
。
首先,永遠不要寫這樣的代碼。以純字符串的形式構建自己的條件會使您容易受到SQL注入漏洞攻擊。如果必須這樣做的條件,然後再去做這樣的:
:conditions => ["webeeh_project_id = ?", project_id]
,如果你有一個項目的模型,你應該從你Webeeh模式webeeh_project_id
列重命名爲project_id
和有像has_many :webeehs
然後,您將不需要再撥打該電話,只需執行p = Project.find(id)
,然後p.webeehs
將返回所需的webeeh。
結果將是一個可以迭代的數組。並讓你的webeeh.webeeh_date
成員,只是把它像這樣:
result.each do |webeeh|
date = webeeh.webeeh_date
end
webeeh_result通常是數據庫結果數組。
您可以重複使用throughit
webeehs_result.each do |webeeh| # use "webeeh.webeeh_date" to access the column_name or do whatever you want with it. end
webeehs_result = Webeeh.findwebeeh_dates
是足以讓所有columnn值。
對於不同的方法和性能問題,請檢查以下內容:http://www.stopdropandrew.com/2010/01/28/finding-ids-fast-with-active-record.html
我知道。謝謝。 – Norah 2011-01-27 08:46:41