I'm trying series criteria based on alternative rail parameters.
I want to filter with the selected tag as well as based on search.
Here is the current code that works under all circumstances:
if paramount [: tags] and parameters [: search] @ notes = note: tagged_with_criteria (params [: Tag]). Elsef Paramos [Params [: Search]] Elsip Params [: Tag] @ Notes = Note: Tagged_with_criteria (Paramoz [: Tag]) Elsif Param [[Search] @ Notes = Note. Full_text_shatch (Param [: search]) I only
@notes = Note.tagged_with_criteria (params [: tag]) Full_text_search (params [: search]) If without the statement, but only if one parameter was given, all notes have been returned.
Note that each parameters (tag_grid_truttias and full_test_scheck) are returning notecatteria if their parameters are zero / empty
Is there a simpler and more compelling method for such things?
I was attacking one-on-one criteria rather than weird "If the consultation [...] and the counsel [...]" thing ..
< Strong> Update: The current methods are:
def tag_ with_criteria (_tags) _tags = [_tags] until _tags.is_a? Arrays if _tags.empty? Other criteria for criteria (. Tag = & gt; _tags) end-end self self.full_text_search (query) if the query regex = / # {query} / # the string supplied is valid regex (without forward slash) - use it as such Do not include criteria (where:: content = & gt; regex) Rescue # Not valid regexp - as a literal search string parameter -Tit. ((Content = & gt; (/#{Regexp.escape(query)}/)) End & Show # All notes if no search parameter criteria end end
In such a situation, the scope will not be modified to do anything when I pass in empty values.
It seems that it may be that you are getting an empty string from the parameter hashes, which your code thinks that something was recorded. Try the scope with.
def tag_ with_criteria (_tags) _tags = Array.wrap (_tags) .reject (& amp;: blank?) If _tags.empty? Criteria other criteria. (: Tag = & gt; _tags) end end def self.full_text_search (query) query.present If the string supplied to? Regex = / # {query} / # is valid regex (without the forward slash) - it is such a form of criterion Where (: content = & gt; regex) defaults # valid regexp -treat not literal search string criteria where no search parameter criteria ending is ending (: Content = & gt; (/#{Regexp.escape(query)}/)) end #html>
Comments
Post a Comment