I have a Company model that has many industries and a level.
I’ve got a helper function that gets the number of industries based on the level.
Say a Company model instance with “Basic” level should only be searchable for the first 2 industries, although it might contain more, I only want it to show up in results for the first 2 industries.
In the model I have
searchable do
integer :industry_ids, multiple: true
How do I limit the search for the model instance, so that only a specific number of industries can be searched for based on the level as determined by the num_of_industries helper function.
something like
searchable do
integer :industry_ids[num_of_industries], multiple: true
EDIT:
I figured it out
LIMITED_INDUSTRIES_BASED_ON_LEVEL = {
'Free' => 1,
'Silver' => 3,
'Gold' => 5,
'Platinum' => 5
}
def industries_limited
self.industries.limit(LIMITED_INDUSTRIES_BASED_ON_LEVEL[self.level])
end
searchable do
integer :industry_ids, multiple: true do
self.industries_limited.map(&:id)
end
end
Figured it out
Company’s level is say “Free”, and is thus limited to 1 industry, but it can add more, so in the future if it decides to upgrade, all of the information is there. So I was limiting the search itself to basically only count the industries based on the level (in this case only count the first 1, since for level = “Free”, the limit is 1)