Search code examples
ruby-on-rails-5polymorphic-associations

Query in polymorphic association - Rails 5.1


I would like to made a query on a polymorphic association :

@pois = Poi.where(poitable: Sleep.where(track_id: @track.id))

Fine, there works... and I would like add a second argument :

@pois = Poi.where(poitable: Sleep.where(track_id: @track.id)).or.where(poitable: Town.where(track_id: @track.id))

Now, I get an error : "wrong number of arguments (given 0, expected 1)"

What's wrong ?


Solution

  • The or method takes an argument that's another ActiveRecord query

    So the syntax would be :

    Poi.where(poitable: Sleep.where(track_id: @track.id)).or(Poi.where(poitable: Town.where(track_id: @track.id)))