Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array to ActiveRecord::Relation

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?

like image 497
bilal Avatar asked Oct 22 '25 20:10

bilal


1 Answers

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.

like image 102
Martin Gerez Avatar answered Oct 24 '25 09:10

Martin Gerez