Jepzen
Jepzen

Reputation: 3162

List all unique associations active record

I have a class named Job and one named Hour

Job has many Hours

At some point I have selected a collection of Hours. Now I want to get a list of all the Jobs these hours belongs to.

Upvotes: 0

Views: 608

Answers (1)

Arun
Arun

Reputation: 885

There are many ways to get hours.

If your collection is on AR relation then you can do this

<hours collection>.includes(:job).map(&:job)

If it is array of hours then simply do

<hours collection>.map(&:job)

You can also achieve this in sub query(hours collection should be on AR relation)

Job.includes(:hours).where(:id => <hours collection>.select("job_id"))

Upvotes: 2

Related Questions