Guest User

Untitled

a guest
Jan 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. ## 2 queries, union in ruby :(
  2.  
  3. # this works, but it requires making the query and can't build a relation.
  4. class Baz < ActiveRecord::Base
  5. def foos
  6. baras_foos = Foo.joins(:baras).where(:baras => {:baz_id => id})
  7. barbs_foos = Foo.joins(:barbs).where(:barbs => {:baz_id => id})
  8.  
  9. bar_a_foos | bar_bs_foos
  10. end
  11. end
  12.  
  13. ## attempt at the relation approach :|
  14.  
  15. # this sort of works, but there are many duplicate records.
  16. class Baz < ActiveRecord::Base
  17. def foos
  18. Foo.joins(:baras, :barbs).where(Bara.arel_table[:baz_id].eq(id).or(Barb.arel_table[:baz_id].eq(id)))
  19. end
  20. end
  21.  
  22. ## the proper relation approach :)
  23.  
  24. ?
Add Comment
Please, Sign In to add comment