Ruby on rails Rails-从has_many:through模型中选择额外字段
我有以下结构Ruby on rails Rails-从has_many:through模型中选择额外字段,ruby-on-rails,has-many-through,Ruby On Rails,Has Many Through,我有以下结构 class Project has_many :teammates has_many :users, :through => :teammates end class Teammate belongs_to :user belongs_to :project end class User has_many :teammates has_many :users, :through => :teammates end 我在加入模型中添加了一个额
class Project
has_many :teammates
has_many :users, :through => :teammates
end
class Teammate
belongs_to :user
belongs_to :project
end
class User
has_many :teammates
has_many :users, :through => :teammates
end
我在加入模型中添加了一个额外的字段:队友->待定:布尔值
我想做的是显示一个项目,其中包括使用布尔值的用户,以了解每个用户是否仍处于挂起状态
编辑:查询如下所示:
Project.includes(:users).find(params[:id])
@project = Project.find(params[:id])
@project.teammates.each do |teammate|
# an example provided to show that you have access to `pending` field as well as all users fields:
teammate.pending # pending field
teammate.user # all user fields
end
我希望能够做到以下几点:
class Project
has_many :teammates
# I tried this
has_many :users, :through => :teammates, :select => 'users.*, teammates.*'
# ERROR: Unknown key: :select
# And this
has_many :users, -> { select("users.*, teammates.pending") }, through: :teammates
# ERROR: missing FROM-clause entry for table "teammates"
end
我会这样做:
Project.includes(:users).find(params[:id])
@project = Project.find(params[:id])
@project.teammates.each do |teammate|
# an example provided to show that you have access to `pending` field as well as all users fields:
teammate.pending # pending field
teammate.user # all user fields
end
你的问题是什么?问题是当我进行查询时,只有用户被抓取。布尔值不是结果的一部分。您的查询是什么样子的?我编辑了问题它不会解决您的问题,但有很多问题:班上缺少队友用户。