我有三张桌子在栏杆里
这是桌子
Employee =(根本没有关联)
用户=有许多出席者
考勤=属于用户
那么,我的问题是如何用这个查询这三个表。
User表加入考勤,然后也加入Employee其中employee.code = users.Empkey
如何将其转换为在一个查询中与rails 5连接三个表?
发布于 2018-08-26 01:10:09
您应该在员工和用户之间创建ActiveRecord关联。
class Employee < ApplicationRecord
belongs_to :user, foreign_key: "code", class_name: "User"
end
class User < ApplicationRecord
has_many :attendances
has_one :employee, foreign_key: "Empkey", class_name: "Employee"
end
class Attendance < ApplicationRecord
belongs_to :user
end现在你可以随便玩了。
employee = Employee.first
@employee_attendances = employee.user.attendances发布于 2018-08-26 07:44:41
可以在联接表中使用原始sql连接3个表,如
User.joins(:attendances).joins("Inner joins employees on employees.code = users.Empkey")希望这能帮上忙
https://stackoverflow.com/questions/52018828
复制相似问题