繁体   English   中英

查询具有数组数据类型的Rails Postgres

[英]Query Rails Postgres with array data type

因此,我一直试图在Rails中查询我的PostgreSQL模型,但得到以下错误:

未定义的方法id为TransactionTemplate :: ActiveRecord_Relation

我的代码:

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

我知道事务模板是一个数组,因此,不仅有一个它需要查找的ID,而且还有多个ID,就像我想要的那样。

有什么建议么?

尝试这个:

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

如果您只需要ID,请尝试:

@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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM