2016-05-05 81 views
0

所以Postgres的,我一直試圖查詢在Rails的我的PostgreSQL的模式,但出現以下錯誤:查詢導軌和數組數據類型

undefined method `id' for TransactionTemplate::ActiveRecord_Relation

我的代碼:

@transaction_templates = TransactionTemplate.where("transaction_category_id = 1") 
    @transaction = Transaction.where("transaction_template_id in (?)", @transaction_templates.id) 

我知道,事務模板是一個數組,因此不僅僅需要查找一個ID,而且還需要多個ID,就像我想要的那樣。

有什麼建議嗎?

回答

2

試試這個:

@transaction_templates = TransactionTemplate.where("transaction_category_id = 1") 
@transaction = Transaction.where("transaction_template_id in (?)", @transaction_templates.map(&:id)) 
+0

迷死人!謝謝,它像魔術一樣工作! – doer123456789

+0

@AdamEsterhuizen很高興它爲你工作! :) – dp7

0

如果你只需要IDS然後嘗試:

@transaction_template_ids = TransactionTemplate.where("transaction_category_id = 1").pluck(:id) 
@transaction = Transaction.where("transaction_template_id in (?)", @transaction_template_ids) 

或者你有適當的關聯,然後

@transaction = Transaction.joins(:transaction_template).where("transaction_category_id = 1") 
+0

謝謝安薩爾,雖然使用dkp的答案 – doer123456789