【问题标题】:Selecting Records where the ID is in a list of ID stored as an attribute选择 ID 在作为属性存储的 ID 列表中的记录
【发布时间】:2018-07-30 14:58:24
【问题描述】:

我的数据库中有这些表,Locations 和 Services

每个Location 记录都有一个名为location_ids 的属性,它是一个以逗号分隔的id 列表*。

*我这样表示我的多对多关系,因为我有很多类似于Services 的表,并且不想拥有大量的链接表

我的桌子

CREATE TABLE services (
  id bigint(20) UNSIGNED NOT NULL,
  location_ids varchar(255) DEFAULT NULL,
  image varchar(255) DEFAULT NULL,
  created_at timestamp NULL DEFAULT NULL,
  created_by bigint(20) UNSIGNED NOT NULL DEFAULT '0',
  updated_at timestamp NULL DEFAULT NULL,
  updated_by bigint(20) UNSIGNED DEFAULT NULL
);

CREATE TABLE locations (
  id bigint(20) UNSIGNED NOT NULL,
  street1 varchar(255) DEFAULT NULL,
  street2 varchar(255) DEFAULT NULL,
  city varchar(255) DEFAULT NULL,
  province varchar(255) DEFAULT NULL,
  country varchar(255) DEFAULT NULL,
  postal_code varchar(255) DEFAULT NULL,
  phone1 varchar(45) DEFAULT NULL,
  phone2 varchar(45) DEFAULT NULL,
  fax varchar(255) DEFAULT NULL,
  email varchar(255) DEFAULT NULL,
  twitter_url varchar(255) DEFAULT NULL,
  facebook_url varchar(255) DEFAULT NULL,
  created_at timestamp NULL DEFAULT NULL,
  created_by bigint(20) UNSIGNED NOT NULL DEFAULT '0',
  updated_at timestamp NULL DEFAULT NULL,
  updated_by bigint(20) UNSIGNED DEFAULT NULL
);

我正在尝试检索所有Services,其中特定位置的ID(我从我的参数中获得)在location_ids 属性中。

这是我的查询(不起作用),它可能会使我试图检索的内容更清楚

Admin::Service.where("#{params[:location_id]} in (services.location_ids)")

*params[:location_id] 将是当前位置的 id

任何帮助将不胜感激!

更新

我结束在我的数据库中添加 5 个链接表,通过 Rails 使其更容易和更易于访问。 感谢所有帮助过的人

【问题讨论】:

  • 看起来服务没有名为 location ids 的列,它的位置 id
  • 这实际上是一种抱歉,我已修复它
  • 您基本上只能执行 SQL LIKE 查询来获取在 location_id 字符串中某处具有 id 的所有行。但我建议不要走这条路,原因有两个:1)会很慢,因为LIKE查询不能使用索引。 2) 您将不得不处理正在搜索 ID %123% 找到具有 location_id 的记录的案例,如下所示:1234, 4567。我的建议是:不要与 Rails 约定冲突,使用常见的has_and_belongs_to 关系。
  • 这是否意味着为我的关系使用 link_tables 会更简单、更高效,即使这意味着拥有更多表?
  • 是的,这会更容易实现(因为它遵循 Rails 约定)并且性能会更好!

标签: mysql ruby-on-rails ruby


【解决方案1】:

我同意@spickermann。我强烈建议您使用 has_and_belongs_to_many 或 has_many :through 关系。无论如何,我相信这应该可行(不推荐)。

Admin::Service.all.select{ |service| 
    service.location_ids.split(',').map(&:to_i).include?(params[:location_id])
}

【讨论】:

    【解决方案2】:

    我最终在我的数据库中添加了链接表。添加它们所花费的工作远远少于我在项目后期所做的工作。

    我使用的示例

    对于我的服务,我创建了一个名为 location_services 的表,其属性为 ID、location_id、service_id。 在我的 Rails 服务模型中,我做到了

    在我的服务模型中

    class Service < ApplicationRecord
        has_many location_services, dependent: :destroy
        has_many :locations, through: :location_services
        accepts_nested_attributes_for :location_services
    end
    

    在我的 LocationService 模型中

    class LocationService < ApplicationRecord
         belongs_to :location, optional: true
         belongs_to :service, optional: true
    end
    

    在我的位置模型中

    class Location < ApplicationRecord
        has_many :location_services, dependent: :destroy
        has_many :services, through: :location_services
    end
    

    然后,我对与我的 locations 表具有多对多关系的每个表重复这些步骤

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 2015-06-02
      • 2017-03-04
      • 2015-08-21
      相关资源
      最近更新 更多