ajax-datatables-rails icon indicating copy to clipboard operation
ajax-datatables-rails copied to clipboard

how to use view_columns with one class associated twice

Open MiroBabic opened this issue 6 years ago • 5 comments

Hello, this is my model

class Associate < ActiveRecord::Base
	has_many :missions
	has_many :coordinator_missions, :class_name => "Mission", :foreign_key  => "coordinator_id"
end
class Mission < ActiveRecord::Base
  belongs_to :associate
  belongs_to :coordinator, :class_name => "Associate"
end

Showing data in table with Mission.joins(:associate).joins(:coordinator) works fine, but I cannot search because of Coordinator this is my datatable code

class MissionDatatable < AjaxDatatablesRails::Base

 
  def view_columns
    @view_columns ||= {
       id: { source: "Mission.id", cond: :eq },
       fy: {source: "Mission.fy"},
       associate_email: {source: "Associate.email"},
       associate_id: {source: "Mission.associate_id"},
       coordinator_email: { source: "Coordinator.email" },
       coordinator_id: { source: "Mission.coordinator_id" },
       name: { source: "Mission.name" },
       fye_output: { source: "Mission.fye_output" },
       status: { source: "Mission.status" },
       created_at: { source: "Mission.created_at" },
       updated_at: { source: "Mission.updated_at" }
    }
  end



  def data
    records.map do |mission|
      {
        # example:
         id: mission.id,
         fy: mission.fy,
         associate_email: mission.associate.email,
         associate_id: mission.associate_id,
         coordinator_email: mission.coordinator.email,
         coordinator_id: mission.coordinator_id,
         name: mission.name,
         fye_output: mission.fye_output,
         status: mission.status,
         created_at: mission.created_at,
         updated_at: mission.updated_at
        }
    end
  end

  private



def get_raw_records
  Mission.joins(:associate).joins(:coordinator)
end


end

could you pls help me how to do correct setup for one model associated twice? The error in log is "wrong constant name Coordinator"

MiroBabic avatar Jun 28 '18 20:06 MiroBabic

Same problem. These columns should be represented in the ModelName.column_name, or aliased_join_table.column_name notation. I tried to use aliased_join_table.column_name but looks like it doesn't work, or which KEY I should use?

AleksandrLeontev avatar Aug 30 '18 13:08 AleksandrLeontev

I have the same issue. It would be great if this were possible.

alaarab avatar Sep 12 '19 05:09 alaarab

I have the same problem. Any solution? thank you!

rollyar avatar Sep 08 '20 19:09 rollyar

My Solution was to create a fake model, which inherits from the original_table you want to search and give it the table name of the SQL-Alias.

In my case the alias was customers_invoices (which i got from the query in the logs)

class CustomersInvoice < Customer
  self.table_name = 'customers_invoices'
end

And in the datatable I named it

customer_invoice_name: { source: 'CustomersInvoice.name' },

And it worked

Datyv avatar Mar 30 '22 07:03 Datyv