Rails查询有两个以上的连接

时间:2013-08-07 10:57:55

标签: ruby-on-rails ruby-on-rails-3

我需要加入并以json格式从三个表中获取结果

@employee = Employee.joins(:salary,:personal_info).select("salary.*,personal_info.*)  --> This works fine

@employee = Employee.joins(:salary,:personal_info,:loan).select("salary.*,personal_info.*,loan.*)  --> only salary and personal_info is coming

format.json { render json: @employee, serializer: EmployeeSerializer, :root => 'employee'}

1 个答案:

答案 0 :(得分:0)

通过提供原始sql来尝试这个,

@employee = Employee.joins('INNER JOIN salary ON salary.employee_id = employee.id INNER JOIN personal_info ON personal_info.employee_id = employee.id INNER JOIN loan ON loan.employee_id = employee.id')