Tee*_*eej 6 postgresql activerecord ruby-on-rails
关系:
Item belongs to Product
Product belongs to User
Run Code Online (Sandbox Code Playgroud)
项目范围:
scope :search, ->(search_term) {
select('products.name, users.*, products.brand, COUNT(products.id)')
.joins(:product => :user)
.where('users.name = ? OR products.brand = ?', search_term, search_term)
.group('products.id')
}
Run Code Online (Sandbox Code Playgroud)
以上结果在以下SQL语句中:
SELECT products.name, users.*, products.brand, COUNT(products.id) FROM "items"
INNER JOIN "products" ON "products"."id" = "items"."product_id"
INNER JOIN "users" ON "users"."id" = "products"."user_id"
WHERE (users.name = 'Atsuete Lipstick' OR products.brand = 'Atsuete Lipstick')
GROUP BY products.id
Run Code Online (Sandbox Code Playgroud)
这里的问题是发生错误:
ActiveRecord::StatementInvalid: PG::Error: ERROR: column "users.id"
must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT products.name, users.*, products.brand, COUNT(product...
Run Code Online (Sandbox Code Playgroud)
有什么可以解决这个问题?
Ale*_*and -8
从错误中您可以看到您应该尝试users.id
在GROUP BY
子句中包含:
.group('products.id, users.id')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9894 次 |
最近记录: |