Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在查询中有条件地添加或设置条件_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 在查询中有条件地添加或设置条件

Ruby on rails 在查询中有条件地添加或设置条件,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,订单表有几列,比如电子邮件、电话和地址 用户提供电子邮件/电话/地址,其中任何一个都可以是零或空字符串(编辑的) 如何生成OR查询,以便在任何列匹配时返回记录 唯一的问题是,如果提供的任何值为nil或空,则将忽略该值 我可以使用Arel执行以下操作: email = params[:email] tel = params[:tel] address = params[:address] t = Order.arel_table sq = t[:email].eq(email) if emai

订单表有几列,比如电子邮件、电话和地址

用户提供电子邮件/电话/地址,其中任何一个都可以是零或空字符串(编辑的

如何生成OR查询,以便在任何列匹配时返回记录

唯一的问题是,如果提供的任何值为nil或空,则将忽略该值

我可以使用Arel执行以下操作:

email = params[:email]
tel = params[:tel]
address =  params[:address]

t = Order.arel_table
sq = t[:email].eq(email) if email.present?
sq = sq.or(t[:phone].eq(phone)) if phone.present?
sq = sq.or(t[:phone].eq(address)) if address.present?
Order.where( sq )
但是,如果email为nil,则会出错,因为sq不会实例化


我想阻止构造sql字符串,我使用squel gem。

您可以通过以下方法检查您的参数是否为零。因此,请阅读有关Ick gem的内容,并按照此处给出的步骤进行操作,然后您可以在您的案例中使用它,如:

params[:email]。可能

params[:tel]。可能

params[:address]。可能


希望,这就是你要找的。

也许你可以检查一下你的参数是否为零。因此,请阅读有关Ick gem的内容,并按照此处给出的步骤进行操作,然后您可以在您的案例中使用它,如:

params[:email]。可能

params[:tel]。可能

params[:address]。可能

希望,这就是你要找的。

你可以把

Order.where("email=? or tel= ? or address=?", params[:email], params[:tel], params[:address])
你可以把

Order.where("email=? or tel= ? or address=?", params[:email], params[:tel], params[:address])
请试一下

where_condition = "true"
where_condition += "OR email = '#{params[:email]}'" if params[:email].present?  
where_condition += "OR tel = '#{params[:tel]}'" if params[:tel].present?
where_condition += "OR address = '#{params[:address]}'" if params[:address].present?

Order.where(where_condition)
请试一下

where_condition = "true"
where_condition += "OR email = '#{params[:email]}'" if params[:email].present?  
where_condition += "OR tel = '#{params[:tel]}'" if params[:tel].present?
where_condition += "OR address = '#{params[:address]}'" if params[:address].present?

Order.where(where_condition)

为了防止出现nil或空字符串,我最终得到了以下结果:

t = Order.arel_table

conditions = [:email, :phone, :address].map{|attr|
  attr_value = params[attr]
  if attr_value.present?
    t[attr].eq(attr_value)
  else
    nil
  end
}.compact

if conditions.empty?
  Order.where('1=0')
else
  Order.where( conditions.inject{|c, cc| c.or(cc).expr} )
end

丑陋但灵活。

为了防止出现零或空字符串,我最终得到了以下结果:

t = Order.arel_table

conditions = [:email, :phone, :address].map{|attr|
  attr_value = params[attr]
  if attr_value.present?
    t[attr].eq(attr_value)
  else
    nil
  end
}.compact

if conditions.empty?
  Order.where('1=0')
else
  Order.where( conditions.inject{|c, cc| c.or(cc).expr} )
end
丑陋但灵活。

让我惊讶的是(duh!)这适用于零值,因为在sql
address=NULL
中不会获取空地址记录。让我惊讶的是(duh!)这适用于零值,因为在sql
address=NULL
中不会获取空地址记录。