Handling the error - undefined method where for Array 0x000000071923f8
suggest changewhere exists on ActiveRecord::Relation, not on Array. Calling it on the array from map raises undefined method 'where' for Array.
map is what turns the relation into an array. Keep a relation if you still need where.
This example loads active UserProfile rows whose user id is not 10:
UserProfile.includes(user: :profile_pictures).where(active: true).map(&:user).where.not(id: 10)
That fails after map, because map returns an Array.
Join the user and filter in SQL. where.not(id: 10) would filter user_profiles.id. To filter the associated user, qualify the column:
UserProfile.joins(:user).where(active: true).where.not(users: { id: 10 })
joins does not return the associated User objects the way map(&:user) does. It keeps a UserProfile relation and lets you add more SQL. Use map only after the relation is filtered.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents