Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 为销售代表选择区域_Sql_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Sql 为销售代表选择区域

Sql 为销售代表选择区域,sql,ruby-on-rails,ruby,activerecord,Sql,Ruby On Rails,Ruby,Activerecord,我有一个面向销售代表的应用程序。 我试图从两个方面获取不同机构的信息 如果他们是客户 如果他们处于用户(销售代表)结束的状态 因此,我想显示当前用户(销售代表)区域中的所有客户机构。我该怎么做 因为我以前没有这样做过,而且我对rails比较新,所以我不知道该怎么做 以下是我的模型(我缩短了代码): 用户(销售代表) class用户:代表区 有很多:机构,:通过=>:公司代表 拥有多个:代表区域 有很多:公司代表 结束 州 class State < ActiveRecord::Base

我有一个面向销售代表的应用程序。 我试图从两个方面获取不同机构的信息

  • 如果他们是客户
  • 如果他们处于用户(销售代表)结束的状态
  • 因此,我想显示当前用户(销售代表)区域中的所有客户机构。我该怎么做

    因为我以前没有这样做过,而且我对rails比较新,所以我不知道该怎么做

    以下是我的模型(我缩短了代码):

    用户(销售代表)

    class用户:代表区
    有很多:机构,:通过=>:公司代表
    拥有多个:代表区域
    有很多:公司代表
    结束
    

    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
    
    类状态:rep\u区域
    拥有多个:代表区域
    有很多:机构
    可访问属性:代码,:名称
    结束
    
    机构

    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
    
    class机构:company\u代表
    有很多:公司代表
    结束
    
    我建议这样做:

    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