jbox-web / ajax-datatables-rails

A wrapper around DataTable's ajax methods that allow synchronization with server-side pagination in a Rails app
MIT License
585 stars 228 forks source link

Search not working as expected #342

Open ye-lin-aung opened 4 years ago

ye-lin-aung commented 4 years ago

I have many to many relationship with tracks and right holders. This is as follow

Tracks -> RightHolderTracks -> RightHolders

I had join the table with Tracks.includes(:right_holders)

I tried searching with both

vocalists: { source: "rightholders.name"}, # This gives me wrong constant name error
composers: { source: "Track.rightholders.name", cond: :like }, #This only takes Track.name righholders name got missing. 

How can I search the tables?

arlharis commented 4 years ago

Can u post your whole datatable codes? ^^

ye-lin-aung commented 4 years ago

This is the actual Relations ship

We have 5 more classes like this. VocalistTrack => vocalist, ComposerTrack => composer , MusicProducerTrack => music_producer and etc. Which are all related to creator model.

class VocalistTrack < ApplicationRecord
  belongs_to :vocalist, :class_name => "Creator"
  belongs_to :track
  scope :vname, ->(name) { joins(:vocalist).where("creators.name like ?", "%#{name}%") }

end

  def view_columns
    # Declare strings in this format: ModelName.column_name
    # or in aliased_join_table.column_name format
    @view_columns ||= {
      id: { source: "Track.id", cond: :eq},
      name: { source: "Track.name", cond: :like},
      name_mm: { source: "Track.name_mm", cond: :like},
      album_name: {source: "Track.album_id", cond: :like},
      cover: { source: "Track.name", cond: :like, searchable: false},
      vocalists: { source: "Creator.name"},
      composers: { source: "Creator.name" },
      music_producers: { source: "Creator.name" },
      record_levels: { source: "Creator.name" },
      second_studios: { source: "Creator.name" }

      # id: { source: "User.id", cond: :eq },
      # name: { source: "User.name", cond: :like }
    }
  end

  def data
    records.map do |record|
      record_details_path = Rails.application.routes.url_helpers.dashboard_track_path(record)
      record_edit_path =  Rails.application.routes.url_helpers.edit_dashboard_track_path(record)
      actions = ""

      if params[:pricing_id].nil?
        actions =  render_actions(record,record_details_path, record_edit_path,false,true)
      else
        track_pricing = record.track_pricings.find_by_pricing_id(params[:pricing_id])
        if track_pricing.nil?
          track_pricing = TrackPricing.create(:pricing_id => params[:pricing_id], :track_id => record.id)
        end
        actions =  render_track_pricing_actions(record,record_details_path, record_edit_path,false,false,track_pricing)

      end
      {
        # example:

        id: record.gadi_id,
        normal_id: record.id,
        check: render_form_checkbox(record.id),
        name: ("<a href=#{Rails.application.routes.url_helpers.dashboard_track_path(record.gadi_id)}>" + record.name_mm + "</a>").html_safe,
        album_name: ("<a href=#{Rails.application.routes.url_helpers.dashboard_album_path(record.album.gadi_id)}>" + record.album.name_mm + "</a>").html_safe,
        vocalists: (record.vocalists.map{ |it| user(it) }.join(",")).html_safe,
        composers: (record.composers.map{ |it| user(it)}.join(",")).html_safe,
        music_producers: (record.music_producers.map{ |it| user(it)}.join(",")).html_safe,
        record_levels: (record.record_levels.map{ |it| user(it)}.join(",")).html_safe,
        second_studios: (record.second_studios.map{ |it| user(it)}.join(",")).html_safe,
        actions: actions,
        empty: ""

        # id: record.id,
        # name: record.name
      }
    end
  end

  def user record
    ("<a href=#{Rails.application.routes.url_helpers.dashboard_creator_path(record)}>" + record.name + "</a>")
  end

  def get_raw_records
    # insert query here
    # User.all
    if !params[:album_id].nil?
      @tracks = Track.where({:album_id => params[:album_id]})
    elsif !params[:creator_id].nil?
      if params[:me] == "false" || params[:me] == false
        @tracks = Track.all
      else
        @tracks = Creator.find(params[:creator_id]).all_tracks
      end
    else
      if params[:pricing_id].present? and params[:main_pricing_id].blank?
        @tracks = Track.where(:id => Pricing.find(params[:pricing_id]).included_tracks.pluck(:id))
      elsif params[:main_pricing_id].present?
        if params[:inclusive] == "true"
          @tracks = Track.all.where.not(:id => MainPricing.find(params[:main_pricing_id]).tracks.pluck(:id))
        else
          @tracks = Track.all.where.not(:gadi_id => MainPricing.find(params[:main_pricing_id]).tracks.pluck(:gadi_id))
          MainPricing.find(params[:main_pricing_id]).pricings.where.not(:id => params[:pricing_id]).each do |item|
              @tracks = @tracks.where(:id => item.excluded_track_ids)
          end
        end
      else
        @tracks = Track.all
        unless params[:aggregator_ids].blank?
          @tracks.where(:album_id => Album.where(:aggregator_id => params[:aggregator_ids].split(",")).pluck(:id))
        end
      end
    end
    @tracks.includes(:vocalists,:composers,:music_producers,:second_studios,:record_levels).references(:vocalists,:composers,:music_producers,:second_studios,:record_levels)
  end

end
ye-lin-aung commented 4 years ago

@arlharis I can only search vocalist name but can not search others.