2016-09-27 76 views
0

我想獲取從活動記錄特定的列值特定的列值,我寫在視圖的SQL查詢,但我沒有得到的數據... new.html.erb取從活動記錄軌道

<%= f.text_field :pan_number, { value: OfferLetter.where(["candidate_id = (?)",params[:userID]]).select("pan_number").first, placeholder: 'COYP1234IN', class: 'form-control' } %> 

然後,我就按照我的觀點的結果

OfferLetter.where(["candidate_id = ?",17]).select("pan_number") 


OfferLetter Load (0.3ms) SELECT `offer_letters`.`pan_number` FROM `offer_letters` WHERE (candidate_id = 17) 
=> #<ActiveRecord::Relation [#<OfferLetter id: nil, pan_number: "COPYD123IN">]> 

和new.html.page enter image description here 這裏泛卡號碼不顯示

請幫助我,我在紅寶石是新軌道上,並告訴我在哪裏,我是錯的,什麼是問題

+0

嘗試此,<(%)= f.text_field:PAN_NUMBER, {value:OfferLetter.where([「candidate_id =(?)」,params [:userID]])。first.select(「pan_number」)。first,placeholder:'COYP1234IN',class:'form-control'}% > – Navin

+0

當我們where子句中我們得到一個active :: record :: collection時,試着在獲得單個記錄後在那裏查詢像添加。首先它 – Navin

回答

0

您可以使用採摘這一點,看到http://apidock.com/rails/ActiveRecord/Calculations/pluck

在你的情況下,嘗試以下:

OfferLetter.where("candidate_id = ?", params[:userID]).pluck("pan_number").first 

而且

如你是新來的回報率,以下是幫助你前進方式:

寫足夠的法案在模型中的iveRecord查詢僅用於更快的查詢執行,而不是查看,控制器等。

因此,在這種情況下,嘗試在Model中使用類似'get_pan_number'的類方法並在視圖中使用它。

OfferLetter.rb

def self.get_pan_number(cand_id) 
OfferLetter.where("candidate_id = ?", cand_id).pluck("pan_number").first 
end 

並在視圖中使用,如下所示:

new.html.erb

<% pan_no = OfferLetter.get_pan_number(params[:userID]) %> 
<%= f.text_field :pan_number, { value: pan_no, placeholder: 'COYP1234IN', class: 'form-control' } %> 
+0

非常感謝你..這是工作 –