0

So, Ive been trying to query my PostgreSQL model in Rails, but get the following error:

undefined method `id' for TransactionTemplate::ActiveRecord_Relation

My code:

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

I know that the transaction templates is an array and that there is therefor not just one ID it needs to look up, but multiple of IDs, just as I want it.

Any suggestions?

2 Answers 2

2

Try this:

@transaction_templates = TransactionTemplate.where("transaction_category_id = 1")
@transaction = Transaction.where("transaction_template_id in (?)", @transaction_templates.map(&:id))
Sign up to request clarification or add additional context in comments.

Comments

0

If you need just ids then try:

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

Or you have proper associations then

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

1 Comment

Thanks Ansar, used dkp's answer though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.