I have a parent class in rails that inherits from ActiveRecord::Base. I’m trying to implement a freetext search, plus other queries, in that class such that all classes that inherit from it can use it with their own fields, which change based on the model:
#
# in base class
#
class GenericBase < ActiveRecord::Base
named_scope :freetext, lambda { |query|
if query.present?
{ :conditions => [ self.freetext_fields.join(' LIKE ? or '),
( ["%#{query}%"]*self.freetext_fields.size ) ].flatten }
else
{}
end
}
end
#
# in inheriting class
#
class Person < GenericBase
set_freetext_fields %w(firstname lastname username email)
end
# or
class Address < GenericBase
set_freetext_fields %w(street city)
end
#
# in controller
#
def search
@people = Person.freetext(params[:query])
end
In the example above, how do I implement the set_freetext_fields setter to be easily used in all models that inherit from GenericBase? This should be something very similar to set_table_name available in Rails.
I want to implement it in the parent or a mixin module such that the API for inheriting classes will be as clean as possible.
You can implement something like this:
Where
CisGenericBaseclass