2012-01-06 75 views
0

的MySQL查詢在執行中軌下方MySQL查詢修正軌道

cnt = Domainurl.find_by_sql ["SELECT Max(`count`)FROM `domainurls` WHERE `domaindetail_id`= ?",@domain.id] 
urlcr=Domainurl.find_by_sql ["SELECT * FROM `domainurls` WHERE `domaindetail_id` = ? AND `count` = ?",cnt.count] 
urlcr.each do |cr| 
    puts cr.url 
end 

我得到錯誤:

"error is: undefined method `closed?' for nil:NilClass" 

我有點在MySQL弱,任何人都可以指導我糾正我的錯? :)

謝謝:)

回答

2

問題是你的最後一個查詢

urlcr=Domainurl.find_by_sql ["SELECT * FROM `domainurls` WHERE `domaindetail_id` = ? AND `count` = ?",cnt.count] 

嘗試找到2個參數(domaindetail_idcount)的東西,但你只提供count。 (您必須提供domaindetail_id太)

urlcr=Domainurl.find_by_sql ["SELECT * FROM `domainurls` WHERE `domaindetail_id` = ? AND `count` = ?",@domain.id, cnt.count] #observe that I added @domain.id 

可以重寫,像

cnt = Domainurl.where(:domaindetail_id => @domain.id).maximum(:count) # this returns a number 
urlcr=Domainurl.where("domaindetail_id = ? AND count = ?", @domain.id, cnt) 
+0

感謝您的回覆:) 爲以下查詢 **'CNT = Domainurl.where(:domaindetail_id => @ domain.id).maximum(:count) urlcr = Domainurl.where(「domaindetail_id =?AND count =?」,@ domain.id,cnt.count)** ** 仍然出現錯誤: **'「錯誤是:未定義的方法'計數'爲35:Fixnum「'** – Aniruddhsinh 2012-01-06 09:31:03

+0

抱歉,當我第一次發佈錯誤時,我編輯了最後一個查詢,而不是'cnt.count'現在只是'cnt '因爲'最大(:count)'返回一個數字不是對象 – cristian 2012-01-06 09:35:13

+0

現在它的工作 非常感謝你:) – Aniruddhsinh 2012-01-06 09:47:20