JOIN在rails上查询ruby

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JOIN在rails上查询ruby相关的知识,希望对你有一定的参考价值。

我有3个型号:*技师*工作合同*工作合同类型

我想编写一个帮助程序,对数据库进行搜索查询并返回所有数据,即使2连接表之间的共享列为nil,帮助程序在项目的另一端使用,当我运行时它工作得非常好做搜索

这是用户模型:

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中,我认为连接条件存在问题,我认为如果我用左连接或某些重写查询即使nil也会帮助返回所有数据的东西!注意 :

用户:has_many:work_contracts, - > {order'start_on DESC'}和has_many:work_contract_types,通过:: work_contracts

WorkContract:belongs_to:user和belongs_to:work_contract_type

答案

您可以使用左外连接,默认情况下includes可以做到这一点,但它会重命名该列。所以我建议尝试includes而不是join如果没有帮助尝试:

 Technician.joins("LEFT OUTER JOIN work_contracts ON work_contracts.user_id = users.id").joins("LEFT OUTER JOIN work_contract_types ON work_contracts.work_contract_type_id = work_contract_types.id").where(

以上是关于JOIN在rails上查询ruby的主要内容,如果未能解决你的问题,请参考以下文章

ruby 我的博客文章“Rails中更好的条件验证”的片段

Rails 框架上的 Ruby CGI 代码

Ruby/Rails:生成脚手架的代码在哪里

Ruby on Rails - ActiveRecord 关联问题:查询未正确进行(单数与复数?)

ruby 在rails中查询关联

如何在 ruby​​ on rails 中编写嵌套查询?