I am using rails 4. I have a two models- "Company" & "Employees". Company has_many employees. I want to fetch the company and get all the employees in its nested dictionary in API like this,
{"Company":{"name":"companyName",
"total_employees":1000000,
"Employees":[{"name":"xyz"},
{"name":"abc"}]}
In company controller I tried
respond_with(Company.for(@current_user.id))
In company model
scope :for, ->(id) do
Employee.joins(:company).where(:company_id => id).to_a
end
and getting this output
[{"name":"xyz"},{"name":"abc"}]
But I need company's attributes also. Please help.
Employee.joins(:company).select("companies.*").where(:company_id => id).to_a