Last active
August 31, 2020 14:45
-
-
Save Genkilabs/1d35541efd7fd97258b2 to your computer and use it in GitHub Desktop.
Rails 4 Ransacker to build subquery for searching across multiple fields in multiple polymorphic classes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is how it might be used. Create your instance which could be polymorphic or just a class. | |
#Search using the predicate 'in' because the subquery find_term will return an ID of every record that matches. | |
@profile_instance = either a PetProfile or an OwnerProfile | |
@profile_instance.search( { :find_term_in => "has fleas", :other_ransack_term_eq => "foobar" } ).results() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OwnerProfile < Profile | |
ransacker :find_term, :formatter => proc {|val| OwnerProfile.find_term(val.downcase) }, :splat_param => true, :type => :string do | |
arel_table[:id] | |
end | |
def self.find_term(val) | |
my_arel = Arel::Table.new(OwnerProfile.table_name) | |
my_arel.project(my_arel[:id]) | |
.where(Arel::Nodes::InfixOperation.new('LIKE', my_arel[:self_assessment].lower, "%#{val}%") | |
.or(Arel::Nodes::InfixOperation.new('LIKE', my_arel[:friend_assessment].lower, "%#{val}%")) | |
) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PetProfile < Profile | |
ransacker :find_term, :formatter => proc {|val| PetProfile.find_term(val.downcase) }, :splat_param => true, :type => :string do | |
arel_table[:id] | |
end | |
def self.find_term(val) | |
my_arel = Arel::Table.new(PetProfile.table_name) | |
my_arel.project(my_arel[:id]) | |
.where(Arel::Nodes::InfixOperation.new('LIKE', my_arel[:owner_assessment].lower, "%#{val}%")) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Profile < ActiveRecord::Base | |
#... anything you need for the parent class | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could also do this in a has_many type configuration where OwnerProfile has_many PetProfile.
In this case the query would look like
OwnerProfile.search({:find_term_or_pet_profiles_find_term_in => "has_fleas"}).result()