gpt4 book ai didi

ruby-on-rails - 搜索没有最后一个的事件记录

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:34 27 4
gpt4 key购买 nike

我怎样才能得到有特定目标的卖家的名字?遵循以下模型:

enter image description here

我怎样才能得到有特定目标的卖家的名字?我有正确的数据库模型。

我正在尝试这样:

GoalSalesman.where (goal_id: 1).last.salesman.name

但由于last,所以我只得到了姓氏,我想要所有的名字。

我怎样才能得到所有的名字?

最佳答案

尝试将关联设置为:

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

然后您应该能够通过调用获取所有名称

Goal.find(1).salesmen.pluck(:name)

关于ruby-on-rails - 搜索没有最后一个的事件记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46943604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com