查询中是ActiveRecord对象吗?

时间:2011-10-11 20:57:30

标签: ruby-on-rails-3 activerecord

这是更简洁的写作方式:

current_user.products.where(:product_id => product.id).present?

rails有类似的东西吗?

current_user.products.has? product

我知道我能做到:

current_user.products.include? product

...但是会加载所有产品。我正试图从中获得一个聪明的询问。

1 个答案:

答案 0 :(得分:4)

您可以使用exists?方法。像:

current_user.products.exists?(product.id)

可以查看Active Record Query Interface GuideAPI