[英]Searching for the active record without the last
How can I get the names of sellers that have a certain goal? 如何获得具有特定目标的卖家的名字? Follow the model below:
请遵循以下模型:
How can I get the names of sellers that have a certain goal? 如何获得具有特定目标的卖家的名字? I have the right database model.
我有正确的数据库模型。
I'm trying like this: 我这样想:
GoalSalesman.where (goal_id: 1).last.salesman.name
But so I get only the last name because of last
, I wanted all the names. 但是因为
last
我只得到last
,我想要所有的名字。
How can I get all of the names? 我怎样才能得到所有的名字?
Try setting up the associations as: 尝试将关联设置为:
GoalSalesman.rb
belongs_to :goal
belongs_to :salesman
Goal.rb
has_many :goal_salesmen
has_many :salesmen, through: :goal_salesmen
Salesman.rb
has_many :goal_salesmen
has_many :goals, through: :goal_salesmen
You should then be able to get all the names by calling 然后你应该能够通过调用获得所有的名字
Goal.find(1).salesmen.pluck(:name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.