0

I have a joins table employees_roles as follows. I would like to get all employee_ids that has role_id 4, 5. Is it possible to do this through rails console or Is it possible to do that through map

employee_id role_id
1           3
2           4
3           4
4           4
5           5
6           5
7           5
8           4
9           4
10          4
11          4

I tried the following but this gives all the record from employee table

Employee.all.map{|e| e.roles.find_all {|role| [4, 5].include? role.id}}
Sam
  • 5,040
  • 12
  • 43
  • 95
  • Sam, check this answer https://stackoverflow.com/questions/52297743/how-do-i-dynamically-chain-where-clauses-in-rails/52299762#52299762 .. I think your question is same as that linked one. – Arup Rakshit Sep 14 '18 at 12:03

1 Answers1

1

you can definitely do this but you'll need to create a class for your DB table, so you can query it using ActiveRecord.

class EmployeesRole < ApplicationRecord
Ben Toogood
  • 469
  • 4
  • 9