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

Search using a polymorphic association

Open safdarzeeshan opened this issue 7 years ago • 1 comments
trafficstars

#models
class Message < ActiveRecord::Base
   belongs_to :recipient, polymorphic: true
   belongs_to :person, foreign_key: 'recipient_id', conditions: "message.recipient_type = 'person'"
end

class Person < ActiveRecord::Base
   has_many :messages, as: :recipient
end

#datatable

def searchable_columns
   @searchable_columns ||=['Person.name']
end

def get_raw_records
   Message.includes(:person).where(id: options[:message_ids])
end

I get different errors based on what I try. ERROR: missing FROM-clause entry for table "persons" or uninitialized constant Person

I've tried changing my get_raw_records to Message.includes(:recipient).where(id: options[:message_ids]) also

Any ideas on how to search with a polymorphic association

safdarzeeshan avatar Aug 24 '18 07:08 safdarzeeshan

Join the tables dude.

miyamotokenji avatar Sep 10 '18 05:09 miyamotokenji