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