mercredi 12 avril 2017

sort_link with Polymorphic Relationship + Ransack

I have one polymorphic relationship

class Offer < ApplicationRecord
  belongs_to :offerable, polymorphic: true
end

class Company < ApplicationRecord
  has_many :offers, as: :offerable, dependent: :destroy
end

class Location < ApplicationRecord
  has_many :offers, as: :offerable, dependent: :destroy
end

Now Offers are offerable with either Company or Location.

In the both tables (Company, Location), name field is there in the database.

= sort_link(query, :offerable_of_Company_type_name) do
   %span{ class: ["btn btn-outline-success btn-sm", ('active' if params[:name])] } Name

Above code is working but I want to search with Company name as well as Location name so I will try below

= sort_link(query, :offerable_of_Company_type_name_or_offerable_of_Location_type_name_cont, 'name') do
  %span{ class: ["btn btn-outline-success btn-sm", ('active' if params[:name])] } Name

But, above code is not working. So how can I sort with two fields for offerable. I want to sort with Company name as well as Location name.

I'm using ransack for searching.

Aucun commentaire:

Enregistrer un commentaire