4

I am using find_by_sql for this query, and it iss returning an array. I want to do something like Job.hongkong_jobs.where(status: true) but I can't because it is an array.

 scope :hongkong_jobs, -> { find_by_sql "SELECT DISTINCT(jobs.*) FROM" + Task.near([22.275754, 114.163056], 35.17936816723013, :units => :km).joins(:job).where(jobs: {status: [:open,:new]}).to_sql.split("FROM").last.split("ORDER").first + "ORDER BY start_at ASC" }

So my question is : how can I convert it into an active record object so I can do .where within this scope?

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
bilal
  • 219
  • 1
  • 5
  • 15

3 Answers3

6

I tried with Job.where(aray.map(&:id)), and it's not working for me.

My solution is:

Job.where(id: array.map(&:id))

This works.

Cheers.

Robert
  • 504
  • 5
  • 27
Martin Gerez
  • 91
  • 1
  • 6
1

Given an array of Active Records, you can obtain the ActiveRecord::Relation by first obtaining the ids of the records using the map function, then use .where to fetch them from the model.

In your case:

Job.where(array.map(&:id))

Son D. Ngo
  • 41
  • 5
0

1 - Do it in SQL: put it in the SQL query you have above as raw SQL

OR

2 - Do the sorting in ruby: Use select on your array to get the records that would match your criterion (all the ones that have a true status)

born4new
  • 1,677
  • 12
  • 12