【问题标题】:Searching for the active record without the last搜索没有最后一个的活动记录
【发布时间】:2017-10-25 23:35:19
【问题描述】:
如何获取有特定目标的卖家名称?遵循以下模型:
如何获取有特定目标的卖家名称?我有正确的数据库模型。
我正在尝试这样:
GoalSalesman.where (goal_id: 1).last.salesman.name
但是因为last,所以我只得到了姓氏,我想要所有的名字。
我怎样才能得到所有的名字?
【问题讨论】:
标签:
ruby-on-rails
ruby
activerecord
ruby-on-rails-5
【解决方案1】:
尝试将关联设置为:
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)