选择销售代表区域

时间:2012-10-30 15:21:38

标签: sql ruby-on-rails ruby activerecord

我有一个销售代表的应用程序。 我试图根据2件事情提取不同的机构信息

  1. 如果他们是客户
  2. 如果他们处于用户(销售代表)结束的状态。
  3. 所以我想在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
    

1 个答案:

答案 0 :(得分: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