【发布时间】:2017-03-05 11:45:07
【问题描述】:
我有一个从“后台”创建的自定义列表,我在其中添加了我想从数据库表中检索的列。这变成了一个数组。好吧,我只想从与 Id 匹配的行中检索那些列。
这是我的代码:
def load_import_contacts
@import_contacts_column_filter = current_user.current_organization.import_columns.split(/,/) # the "custom filter" that retrieve columns
@import_contacts = UserImport.select(@import_contacts_column_filter).where(organization_id: current_user.current_organization.id) # the query
# here all ok, objects filtered how i want.
@import_contacts_listing = smart_listing_create(
# here comes the error message
:import_contacts,
@import_contacts,
partial: 'contacts/listing_import',
default_sort: {created_at: "desc"}
)
end
错误信息:
标题
ActiveRecord::StatementInvalid in ImportsController#index
正文
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' name, surname1, surname2, phone, adress, city, zipcode, zone, birthdate, firstv' at line 1: SELECT COUNT(reference_number, name, surname1, surname2, phone, adress, city, zipcode, zone, birthdate, firstvisit, mutua, organization_id) FROM `user_imports` WHERE `user_imports`.`organization_id` = 279
更多细节
Extracted source (around line #15):
第 15 行:
@import_contacts_listing = smart_listing_create(
【问题讨论】:
-
请求对多列进行一次计数是否有效?我想不是。我认为每个字段都应该有
count()... -
什么意思?
-
SELECT COUNT(reference_number, name, surname1, surname2, phone, adress, city, zipcode, zone, birthdate, firstvisit, mutua, organization_id)。这是有效的 sql 语法吗?我认为不会。 -
我的意思是你要么选择所有字段的计数,
count(*),要么选择一个字段count(id),但你不能选择count(field1, field2, ...),除非我非常错误......跨度> -
我无法选择字段数组?
标签: mysql ruby-on-rails activerecord smart-listing