rails 奇怪的 joins

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.

http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables

你可能感兴趣的:(rails 奇怪的 joins)