rails 奇怪的 joins
@wish_products = WishProduct.joins(:product).where("product_id = products.id AND customer_id = ?",current_customer.id).order('created_at DESC')
注意这里的 joins(:products) 还有以后的 products.id (复数!)
12.3 Specifying Conditions on the Joined Tables You can specify conditions on the joined tables using the regular Array and String conditions. Hash conditions provides a special syntax for specifying conditions for the joined tables:
time_range = (Time.now.midnight - 1.day)..Time.now.midnight Client.joins(:orders).where('orders.created_at' => time_range) An alternative and cleaner syntax is to nest the hash conditions:
time_range = (Time.now.midnight - 1.day)..Time.now.midnight Client.joins(:orders).where(orders: {created_at: time_range}) This will find all clients who have orders that were created yesterday, again using a BETWEEN SQL expression.