JOIN在rails上查询ruby

时间:2018-05-02 15:02:33

标签: jquery ruby-on-rails database join activerecord

我有3个型号:  *技术员  * WorkContract  * WorkContractType

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

这是用户模型:

export function withTracker(func: () => {}): (c: React.Component) => React.Component;

这里是WorkContractType模型:

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
}

和最后一个WorkContract:

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
}

我正在调用我的辅助函数:

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
}

当尝试搜索不起作用的技术人员时,当我删除连接条件时它起作用但不在其他vue中,我认为连接条件存在问题,我认为如果我用左边重写查询加入或返回所有数据的东西,即使nil会有所帮助! 请注意:

  

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

     

WorkContract:belongs_to:user和belongs_to:work_contract_type

1 个答案:

答案 0 :(得分:0)

您可以使用左外连接,默认情况下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(