Query records through its belongs_to relation in Rails

后端 未结 3 1880
悲&欢浪女
悲&欢浪女 2020-12-14 02:06

I have an Activities model, and they belong_to a Location

How do i select all the activities whose location.country = Australia? (for example)

Can I do this

3条回答
  •  青春惊慌失措
    2020-12-14 02:57

    Yes, a scope can be used. Something like this ought to work on the Activities model:

    scope :down_under, 
        joins(:locations).
        where("locations.country = 'Australia')
    

提交回复
热议问题