【问题标题】:ActiveRecord, select query for an Array returns ActiveRecord::StatementInvalid in ImportsController#indexActiveRecord,选择查询数组返回 ImportsController#index 中的 ActiveRecord::StatementInvalid
【发布时间】: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


【解决方案1】:

奇怪的解决方案

当我在智能列表之前添加此行时

**@import_contacts.each do |before|
end**

一切正常,很奇怪,我不知道为什么会这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多