Rails AjaxDatatablesRails::ActiveRecord LEFT JOIN 到同一模型导致搜索错误

问题描述 投票:0回答:1
交易模型有两个引用仓库模型对象的字段。这导致搜索过程中出现错误。按任何这些字段进行搜索或排序仅使用第一个 left_join 中的数据。

class Transaction < ApplicationRecord belongs_to :stored_product belongs_to :user belongs_to :from_stockroom, class_name: 'Stockroom', foreign_key: 'from_stockroom_id', optional: true belongs_to :to_stockroom, class_name: 'Stockroom', foreign_key: 'to_stockroom_id', optional: true attr_accessor :user_selection


    

ruby-on-rails ruby activerecord datatable
1个回答
0
投票
我尝试了很多方法来修复这个错误。最终我能够做到。我将在这里留下帖子以防其他人遇到同样的错误。或者如果有人知道更好的方法来解决它。自定义 AjaxDatatablesRails filter_records(records) 方法并在 get_raw_records 中使用 AS 来处理我的请求对我有用。

class TransactionsDatatable < AjaxDatatablesRails::ActiveRecord extend Forwardable include Rails.application.routes.url_helpers def_delegators :@view, :current_user def initialize(params, opts = {}) @view = opts[:view_context] super end def view_columns @view_columns ||= { id: { source: 'Transaction.id', cond: :eq, searchable: true }, stored_product_id: { source: 'StoredProduct.name', searchable: true }, user_id: { source: 'User.name', cond: :like, searchable: true }, from_stockroom_id: { source: 'from_stockroom_name', cond: :like, searchable: true, orderable: true }, to_stockroom_id: { source: 'to_stockroom_name', cond: :like, searchable: true, orderable: true }, quantity: { source: 'Transaction.quantity', searchable: false, orderable: false }, comment: { source: 'Transaction.comment', cond: :like, searchable: true, orderable: false }, created_at: { source: 'Transaction.created_at', searchable: false } } end def data records.map do |record| { id: record.id, stored_product_id: record.stored_product.name, user_id: record.user.name, from_stockroom_id: record.from_stockroom.present? ? record.from_stockroom.name : 'Поставка', to_stockroom_id: record.to_stockroom.present? ? record.to_stockroom.name : 'Списание', quantity: record.quantity, comment: record.comment, created_at: record.created_at.strftime('%d.%m.%Y'), DT_RowId: record.id } end end def get_raw_records Transaction.where(deleted: false) .joins(:stored_product, :user) .joins('LEFT JOIN stockrooms AS from_stockrooms ON from_stockrooms.id = transactions.from_stockroom_id') .joins('LEFT JOIN stockrooms AS to_stockrooms ON to_stockrooms.id = transactions.to_stockroom_id') .select('transactions.*, from_stockrooms.name as from_stockroom_name, to_stockrooms.name as to_stockroom_name') end def filter_records(records) to_search_value = params[:columns]["4"]["search"]["value"] from_search_value = params[:columns]["3"]["search"]["value"] records = records.where("LOWER(from_stockrooms.name) LIKE ?", "%#{to_search_value.downcase}%") if to_search_value.present? records = records.where("LOWER(to_stockrooms.name) LIKE ?", "%#{from_search_value.downcase}%") if from_search_value.present? records = records.where(build_conditions) return records unless search_value.present? records.or(records.where("LOWER(to_stockrooms.name) LIKE ?", "%#{params["search"]["value"].downcase}%")) .or(records.where("LOWER(from_stockrooms.name) LIKE ?", "%#{params["search"]["value"].downcase}%")) end end
    
© www.soinside.com 2019 - 2024. All rights reserved.