通过关联在Ruby中检索Ruby的数据



我有三个表用户(包含用户详细信息)用户_shifts(包含用户shift映射)和shift(它的ID在users_shift表中)。我正在做的是向用户分配多个班次,我想从用户,用户_shift和shift表中检索所有数据。我做了以下操作:

在用户模型中:

has_many :users_shifts, :dependent => :destroy

在用户shift模型中:

has_one :shift
belongs_to :user

在Shift Model

belongs_to :users_shift

在控制器中我有:

User.all

现在,我想在表格中获取所有数据。我必须展示用户和他的转变。用户详细信息将来自用户表,换档细节将来自Shifts表。users_shift用于用户移动映射。

尝试在您的控制台首次尝试

@user_shifts = @user.users_shifts # by using this you get all users_shifts details for that particular user.
@shift = @user_shift.shift # by using this you will get shift for that paricular user_shift

同样的方式也可以

@shift.user_shift # for getting details for users_shifts
@user_shifts.user # for getting details about user

最新更新