【发布时间】:2018-05-02 15:02:32
【问题描述】:
我有 3 个模型: * 技术员 * 工作合同 * 工作合同类型
我想编写一个助手,对数据库进行搜索查询并返回所有数据,即使 2 个连接表之间的共享列为零,助手用于项目的另一端,它工作得很好好吧,当我进行搜索时
这是用户模型:
class User < ActiveRecord::Base {
:id => :uuid,
:email => :string,
:encrypted_password => :string,
:reset_password_token => :string,
:reset_password_sent_at => :datetime,
:remember_created_at => :datetime,
:sign_in_count => :integer,
:current_sign_in_at => :datetime,
:last_sign_in_at => :datetime,
:current_sign_in_ip => :string,
:last_sign_in_ip => :string,
:confirmation_token => :string,
:confirmed_at => :datetime,
:confirmation_sent_at => :datetime,
:unconfirmed_email => :string,
:authentication_token => :string,
:type => :string,
:company_id => :uuid,
:team_id => :uuid,
:weekly_schedule_id => :uuid,
:first_name => :string,
:last_name => :string,
:avatar => :string,
:username => :string,
:gender => :integer,
:invitation_sent_at => :datetime,
:birthday_on => :date,
:is_archived => :boolean,
:updated_by_id => :uuid,
:device_id => :string,
:import_keys => :hstore,
:created_at => :datetime,
:updated_at => :datetime,
:is_localized => :boolean,
:is_administrator => :boolean,
:store_id => :uuid,
:code => :string,
:current_session_token => :string
}
这里是 WorkContractType 模型:
class WorkContractType < ActiveRecord::Base {
:id => :uuid,
:name => :string,
:slug => :string,
:is_archived => :boolean,
:updated_by_id => :uuid,
:device_id => :string,
:created_at => :datetime,
:updated_at => :datetime
}
最后一个 WorkContract :
class WorkContract < ActiveRecord::Base {
:id => :uuid,
:user_id => :uuid,
:work_contract_type_id => :uuid,
:start_on => :date,
:end_on => :date,
:is_archived => :boolean,
:updated_by_id => :uuid,
:device_id => :string,
:import_keys => :hstore,
:created_at => :datetime,
:updated_at => :datetime,
:rubric_category_id => :uuid
}
我正在调用我的辅助函数:
class TechnicianQueryHelper
def initialize( query = nil )
@query = Arel.sql("UNACCENT('%#{ query }%')")
end
def search
Technician.joins(:work_contract_types).where(
first_name.matches( @query )
.or( last_name.matches( @query ) )
.or( email.matches( @query ) )
.or( technician_code.matches( @query ) )
.or( work_contract_name.matches( @query ) )
)
end
当尝试搜索一个不起作用的技术人员时,当我删除它工作但在其他 vue 中不起作用的连接条件时,我认为连接条件有问题,我认为如果我用 left 重写查询加入或返回所有数据的东西,即使 nil 会有所帮助! 请注意:
用户 : has_many :work_contracts, -> { order 'start_on DESC' } 和 has_many :work_contract_types,通过: :work_contracts
WorkContract:belongs_to:用户和belongs_to:work_contract_type
【问题讨论】:
标签: jquery ruby-on-rails database join activerecord