0

営業担当者向けのアプリケーションがあります。私は2つのことに基づいてさまざまな機関の情報を取得しようとしています

  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 に答える