I agree with @zapico - use scopes. And you must use Lambda because List.good_list_ids
and List.bad_list_ids
are dynamic.
#Book.rb
has_many :lists
scope :good_lists, -> { joins(:lists).where('lists.id IN (?)', List.good_list_ids) }
scope :bad_lists, -> { joins(:lists).where('lists.id IN (?)', List.bad_list_ids) }
Update
now you can get books which are in these lists
# in bad list
Book.bad_lists
# in good list
Book.good_lists
# in both lists (this is intersection! for these lists)
Book.bad_lists.good_lists
If you would like to have records belong to good_list OR bad_list then it seems to me the easiest way is
# join arrays
scope :good_bad_lists, -> { joins(:lists).where('lists.id IN (?)', List.good_list_ids | List.bad_list_ids) }