【问题标题】:Selecting Areas for Sales Reps为销售代表选择区域
【发布时间】:2012-10-30 15:21:38
【问题描述】:

我有一个适用于销售代表的应用程序。 我正在尝试根据两件事提取不同的机构信息

  1. 如果他们是客户
  2. 如果他们处于用户(销售代表)结束的状态。

所以我想在 current_user(销售代表)区域显示所有作为客户的机构。我怎样才能做到这一点?

由于我以前没有这样做过,而且我对 Rails 比较陌生,所以我不知道该怎么做。

这是我的模型(我已经缩短了代码):

用户(销售代表)

class User < ActiveRecord::Base
  has_many :states, :through => :rep_areas
  has_many :institutions, :through => :company_reps
  has_many :rep_areas
  has_many :company_reps
end

国家

class State < ActiveRecord::Base
  has_many :users, :through => :rep_areas
  has_many :rep_areas
  has_many :institutions

  attr_accessible :code, :name
end

机构

class Institution < ActiveRecord::Base
  attr_accessible :company, :phone, :clientdate, :street, :city, :state_id, :zip, :source, :source2, :demodate1, :demodate2, :demodate3, :client, :prospect, :notcontacted
  belongs_to :state
  has_many :users, :through => :company_reps
  has_many :company_reps
end

【问题讨论】:

    标签: sql ruby-on-rails ruby activerecord


    【解决方案1】:

    我建议以这种方式进行:

    states = current_user.states.to_a
    
    # the following are all the Institution record in all the user's areas
    inst_in_states = Institution.where(state_id: states)
    # it will take an array and make an "IN" query
    
    # the following are all the user's own clients, additionally on the states
    # the user is in.
    clients_in_states = current_user.institutions.where(state_id: states)
    # as above, but additionally use the :company_reps join
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-28
      • 2023-03-28
      • 1970-01-01
      • 2016-01-19
      • 2013-11-24
      • 2020-10-15
      • 2014-12-13
      • 2015-11-13
      相关资源
      最近更新 更多