has_many and No method error issue

后端 未结 2 1717
眼角桃花
眼角桃花 2021-01-17 04:17

I have two tables: Stores and products. The store model has a has_many :products and the products has a belongs_to :store

I\'m trying to do

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-17 05:04

    You have it backwards. Since you can have many stores, Rails will not return all the products where open: true.

    You need to join and lookup the products where the store is open.

    Product.joins(:store).where(store: {open: true}).where("created_at <= ?", 1.month.ago)
    

提交回复
热议问题