Rails API 中的链接 .where 方法

问题描述 投票:0回答:1

我有一个使用 Rails API 构建的 Web 应用程序来收集提交内容。我正在提交一个表单,该表单会向具有匹配属性(位置、大小等)的某些商店发送电子邮件线索。它们根据以下字段进行匹配

  • 地点
  • 容量(最小和最大)
  • 商店类型(酒店、餐饮、独特)

我还有一个让商店接收所有潜在客户的属性,如果设置为 true,我想覆盖之前的任何选项。

在我的Rails API 的hard_worker.rb 中,我让它检查以下字段,以便查询将潜在客户发送到哪些商店。我知道条件链接不正确,但无法弄清楚如何重新格式化它以正确发送。任何帮助将不胜感激,谢谢!

def build_filters_obj
        filters = []
        filters.push 'location_north' if @lead.location_north
        filters.push 'location_east' if @lead.location_east
        filters.push 'location_south' if @lead.location_south
        filters.push 'location_west' if @lead.location_west
        filters.push 'location_other' if @lead.location_other
        
        return filters
    end

    def perform(lead_id)
        @lead = Lead.find(lead_id)

        lead_email = ValidEmail2::Address.new(@lead.email)
        UserNotifierMailer.send_signup_email(@lead).deliver if lead_email.valid?

        @stores = Store.all
        @stores = @stores.where.not(email: [nil, ''])

        n = @lead.guests_total.to_i
        @stores = @stores.where("capacity_min <= ? AND capacity_max >= ?", n, n)
        
        @stores = @stores.where(:type_unique => true) if @lead.store_type_unique
        @stores = @stores.where(:type_dining => true) if @lead.store_type_dining
        @stores = @stores.where(:type_hotel => true) if @lead.store_type_hotel
        
        filters = build_filters_obj
        filters.each do |filter|
            @stores = @stores.where(filter.to_sym => true)
        end

        @stores = @stores.or(Store.where(:receive_all => true))

        @stores.each do |store|
            store_email = ValidEmail2::Address.new(store.email)
            UserNotifierMailer.send_lead_email(store, @lead).deliver if store_email.valid?
        end
    end

如果我的代码很糟糕,我深表歉意,Ruby 不是我通常工作的地方,我确信我犯了一些初学者错误!

ruby-on-rails ruby rails-api
1个回答
0
投票

我注意到的最大的代码味道是有很多 if 检查,这使得对代码进行推理变得更加困难。尝试创建哈希并调用方法来清理哈希。

我还建议您通过 rubocop 传递此代码。它会让你的代码感觉更惯用并且更少错误。

  1. build_filters_obj
filters.push 'location_north' if @lead.location_north
filters.push 'location_east' if @lead.location_east
filters.push 'location_south' if @lead.location_south
filters.push 'location_west' if @lead.location_west
filters.push 'location_other' if @lead.location_other

也许我们至少可以让它准备好使用。在 ruby 惯用方式中,将其命名为 noun 并直接使用它。

def location_filters
  {
    location_north: @lead.location_north,
    location_east: @lead.location_east,
    location_south: @lead.location_south,
    location_west: @lead.location_west,
    location_other: @lead.location_other
  }.filter { |key, value| value.present? }
end

# later on you would use this like this

@stores = @stores.where(location_filters)
  1. if 检查 store_type* 相关方法

您可以构建一个哈希,然后删除虚假值

type_filters = {
  type_unique: @lead.store_type_unique
  type_dining: @lead.store_type_dining
  type_hotel: @lead.store_type_hotel
}.filter { |key, value| value.present? }

# later on we can directly pass it to where method

@stores = @stores.where(type_filters)

总的来说,这就是您可以调用查询的方式

def perform(lead_id)
    @lead = Lead.find(lead_id)

    lead_email = ValidEmail2::Address.new(@lead.email)
    UserNotifierMailer.send_signup_email(@lead).deliver if lead_email.valid?

    capacity = @lead.guests_total.to_i

    type_filters = { # consider moving to a private method called `type_filters`
      type_unique: @lead.store_type_unique
      type_dining: @lead.store_type_dining
      type_hotel: @lead.store_type_hotel
    }.filter { |key, value| value.present? }
      
    @stores = Store.where.not(email: [nil, ''])
                   .where("capacity_min <= ? AND capacity_max >= ?", capacity, capacity)
                   .where(type_filters)
                   .where(location_filters)
                   .or(Store.where(:receive_all => true))

    @stores.each do |store|
        store_email = ValidEmail2::Address.new(store.email)
        UserNotifierMailer.send_lead_email(store, @lead).deliver if store_email.valid?
    end
end

https://rubyapi.org/3.2/o/hash#method-i-filter

© www.soinside.com 2019 - 2024. All rights reserved.