Rails 3 join where query - print only on row

61 Views Asked by At

I have two models, Campaigns and Orders

I would like to find a specific campaign with it orders and print all emails.

I tried this in the console but get all the orders data when I only want the row :email

Campaign.find_by_id(46).orders(:email)

How to print only the emails of those orders?

2

There are 2 best solutions below

2
On BEST ANSWER

Try this

Order.where(campaign_id: 46).pluck(:email)

This will return an array of emails of orders with specific email

Hope this helps!

0
On

You can use collect also:

Order.where(campaign_id: 46).collect(&:email)