2010-11-06 74 views
0

我的理解是任何rails對象的find方法都會期望該對象的id列。但我想使用該表的另一列找到該表/對象的一行。我怎麼做 ?如何更改任何對象的「查找」方法的行爲?

比如我有userstats模型如下:

create_table "userstats", :force => true do |t| 
    t.integer "user_id" 
    t.integer "no_of_wts" 
    t.integer "no_of_wtb" 
    t.integer "wts_reputation" 
    t.integer "wtb_reputation" 
    t.integer "overall_reputation" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

我想找到使用USER_ID(我的外鍵的用戶模型)userstat模型,而不是一個實例使用默認userstat模型的唯一關鍵。

有什麼建議嗎?

我用Rails 3.0.1

回答

0

對於你的問題:

UserStat.find_all_by_user_id(5) 

一些的find()其他例子:

UserStat.find_all_by_wts_reputation_and_wtb_reputation(1,2) 
UserStat.find(:all, :conditions => { :no_of_wtb=> 5 }) 
相關問題