You can always use the underlying arel_table:
Plan.where(arel_table[:user_id].not_eq([3,4,7]))
Saving off what the user_ids of [3,4,7] mean might make it a bit clearer
too:
declarative_name = arel_table[:user_id].not_eq(ids)
Plane.where(declarative_name)
Or succinctly in a scope:
scope :declarative_name, ->(ids) {
where(arel_table[:user_id].not_eq(ids)) }
=> Plan.declarative_name [3,4,7]
There is a railscast for promoting the Arel predicates to class level
methods on active_record objects:
So you could end up with:
Plan.match(user_id: { not_in: [3,4,7] })
The draw back is that your interface for all active_record objects is
more
expansive, but it depends on the style of the app and your preferences
on
this matter, I like to keep a minimal public interface personally.
And finally the squeel gem https://github.com/ernie/squeel sprung to
mind
which automagically adds these predicates to active_record objects too:
Plan.where{ user_id.not_in [3,4,7] }
I’ve tried all of these approaches except the railscast version, and
like
access the arel_table directly to build queries the best, as you learn
more
that way