这是更简洁的写作方式:
current_user.products.where(:product_id => product.id).present?
rails有类似的东西吗?
current_user.products.has? product
我知道我能做到:
current_user.products.include? product
...但是会加载所有产品。我正试图从中获得一个聪明的询问。
答案 0 :(得分:4)
您可以使用exists?
方法。像:
current_user.products.exists?(product.id)