sort_link on polymorphic association but with multiple model
dipaktc opened this issue · 3 comments
I have following 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 Now, 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.
This issue appears more to be a HOW-TO issue rather than a bug in Ransack. We are in the process of cleaning up the Ransack gem and repo. If this issue is still relevant could you please ask it on StackOverflow?
If you think it is an issue with Ransack, please provide a repo with the issue and / or a pull request with failing tests. If you don't know how to do this, indicate it in the issue and we will look into it eventually.
Thank you
https://github.com/activerecord-hackery/ransack/blob/master/CONTRIBUTING.md
@dipaktc I am also facing that same issue. Any solution for this issue?
I'm running into the same blocker, but with the added complexity of having different attribute names in the different associations. I have a Notification with a polymorphic 'originator' that can be a Person, Group, or Channel. Person has 'last_name', while Group and Channel have 'name'.
I can sort via both of these sort_links:
sort_link(@page.query, "originator_of_Person_type_last_name, "Originator Name")
sort_link(@page.query, "originator_of_Group_type_name, "Originator Name")
However, I cannot sort by:
sort_link(@page.query, "originator_of_Person_type_last_name_or_originator_of_Group_type_name, "Originator Name")