0

我有三个表 Users(包含用户详细信息) users_shifts(包含用户移位映射)和 shift(它的 id 在 users_shift 表中)。我正在做的是为用户分配多个班次,我想从用户、users_shift 和班次表中检索所有数据。我做了以下事情:

在用户模型中:

has_many :users_shifts, :dependent => :destroy

在 users_shift 模型中:

has_one :shift
belongs_to :user

在班次模型中

belongs_to :users_shift

在控制器中我有:

User.all

现在我想在视图中获取表中的所有数据。我必须显示用户和他的转变。用户详细信息将来自用户表,班次详细信息将来自班次表。users_shift 用于用户班次映射。

4

1 回答 1

1

首先在您的控制台中尝试

@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
于 2012-10-23T11:22:03.503 回答