0

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

  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
4

1 回答 1

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
于 2012-10-30T15:34:17.917 回答