I have a basic has_many relationship, modeled as such:
class Term < ActiveRecord::Base
has_many :acroynms
end
And
class Acronym < ActiveRecord::Base
belongs_to :term
end
I would like to write an arel relation that will pull terms based on the term's name, or an acronym's name. My relation is incorrect, but I'm looking to do something that would look like this:
terms = Term.arel_table
relation = terms[name].matches("%#{params['a']}%")
relation = relation.or(terms[:acronyms.name].matches("%#{params['a']}%"))
@results = Term.where(relation)
I would like to keep it in the relation, because I have many other composed conditions combined into that one relation (that I'm omitting here for brevity).
I'm using the relation, rather than just composing everything together on the final line is that the items that need to composed won't be known until runtime, depending on user input. My thought is using this model I'll be able to set up my relation, however complex it may be, and then just have that one line to set @results, regardless of the user input.
Is there a way to write this all in a relation? Otherwise, I suppose I'll need to resort to using arel's joins, but only join in situations where the user input calls for it, correct?