I have two models Project
and UrlList
. A project :has_many
url_list and a url_list :belongs_to
project.
Now I have array for project id's all_projects = [1,2,5,8,16]
. I want to retrieve all the records from url_list
where project_id
is one of those from all_projects
array. How do I write code for it?
You can pass an array as value for an attribute to where
method:
all_projects = [1, 2, 5, 8, 16]
url_lists = UrlList.where(:project_id => all_projects)
It'll generate SQL query like that:
SELECT `url_lists`.* FROM `url_lists` WHERE `project_id`.`user_id` IN (1, 2, 5, 8, 16)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With