In Employees Index page I have column 'Region Name', with a search field.
Now i need to search column-'Region Name' from 'Employee' table, traversing to 'Region' table
Employee -> Company -> Organization -> Region
Employee table has foreign key of Company(company_id)
Company table has foreign key of Organization(organization_id)
Organization table has foreign key of Region(region_id)
Region table has column 'region_name'
These are my models
class Employee < ActiveRecord::Base
belongs_to :company
belongs_to :organization
end
class Company < ActiveRecord::Base
belongs_to :organization
has_many :employee
end
class Organization < ActiveRecord::Base
belongs_to :region
has_many :company
has_many :employees, through: :companies
end
class Region < ActiveRecord::Base
has_many :companies
has_many :employees, through: :companies
end
How can I search traversing 3 tables and display the list of employees in index page of Employees of that region?
Note: database-Postgresql
You can try this:
Employee.joins(company: {organization: :region}).where(regions: {region_name: 'Your Region Name'})