- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想创建一个查询链,以便根据特定过滤器找到我的机器人用户。一些过滤器可以有多个值。例如,我的“语言环境”过滤器可以有多个值(例如 fr_FR、en_US)。
在此示例中,我选中了两个区域设置复选框(fr_FR 和 en_US)。
我创建了一个查询链,但输出不是我想要的:
SELECT "bot_users".* FROM "bot_users" WHERE ("bot_users"."core_bot_id" = ? AND (locale = 'fr_FR') OR "bot_users"."core_bot_id" = ? AND (locale = 'fr_FR') AND (locale = 'en_EN')) [["core_bot_id", 1], ["core_bot_id", 1]]
我想要这样的东西:
SELECT "bot_users".* FROM "bot_users" WHERE ("bot_users"."core_bot_id" = ? AND (locale = 'fr_FR' OR 'en_EN')) [["core_bot_id", 1]]
代码如下:
@filter = Filter.find_by_letter_id(@letter.id)
$i = 1
$a = 1
$s = 1
query = [{first_name: @filter.first_name}, {last_name: @filter.last_name}, {source: @filter.segment}, {gender: @filter.gender}, {timezone: @filter.timezone}, {locale: @filter.locale}, {created_at: [@filter.creation_date_start, @filter.creation_date_finish]}]
query_chain = BotUser.where(core_bot_id: @bot.id)
query.each do |hash|
hash.each_pair do |key, value|
if value.present? == true
if key.to_s == "timezone"
while $i < value.size do
query_chain = query_chain.where("timezone = ?", value[$i].to_f)
$i += 1
end
elsif key.to_s == "locale"
while $a < value.size do
puts $a.to_s
if $a == 1
query_chain = query_chain.where("locale = ?", value[$a])
else
query_chain = query_chain.or(query_chain.where("locale = ?", value[$a]))
end
$a += 1
end
elsif key.to_s == "gender"
query_chain = query_chain.where("gender = ?", value)
elsif key.to_s == "core_bot_id"
query_chain = query_chain.where("core_bot_id = ?", value)
elsif key.to_s == "created_at"
if value[0].present? == true and value[1].present? == true
query_chain = query_chain.where('created_at BETWEEN ? AND ?', value[0], value[1].end_of_day)
elsif value[0].present? == true
query_chain = query_chain.where('created_at > ?', value[0])
elsif value[1].present? == true
query_chain = query_chain.where('created_at < ?', value[1].end_of_day)
end
else
query_chain = query_chain.where("#{key} = ?", value)
end
end
end
end
private
def filter_params
params.fetch(:query, {}).permit(:first_name, :last_name, :timezone, :gender)
end
def set_nb_recipients
@filter = Filter.find_by_letter_id(@letter.id)
filter_params = ActionController::Parameters.new({
query: {
core_bot_id: @bot.id,
first_name: @filter.first_name,
last_name: @filter.last_name,
source: @filter.segment,
gender: @filter.gender,
timezone: @filter.timezone,
locale: @filter.locale,
creation_date_start: @filter.creation_date_start,
creation_date_finish: @filter.creation_date_finish
}
})
query = FilterQuery.new(filter_params)
query = FilterQuery.new(filter_params)
@bot_users = query.execute || BotUser.none
@nb_users = @bot_users.length
end
class FilterQuery
include ActiveModel::Model
attr_accessor :first_name, :last_name, :timezone, :gender, :locale, :core_bot_id, :source, :creation_date_start, :creation_date_finish
validates :gender, inclusion: { in: %w(male female) }
def initialize(params)
super(params)
end
def execute
return false unless valid?
@bot_users = BotUser.where(core_bot_id: core_bot_id)
@bot_users = @bot_users.where('first_name LIKE ?', "#{first_name}%") if first_name.present?
@bot_users = @bot_users.where('last_name LIKE ?', "#{last_name}%") if last_name.present?
@bot_users = @bot_users.where(timezone: timezone) if timezone.present?
@bot_users = @bot_users.where(timezone: locale) if locale.present?
@bot_users = @bot_users.where(gender: gender) if gender.present?
@bot_users = @bot_users.where(source: source) if source.present?
@bot_users = @bot_users.where('created_at BETWEEN ? AND ?', creation_date_start, creation_date_finish) if creation_date_start.present? and creation_date_finish.present?
@bot_users = @bot_users.where('created_at > ?', creation_date_start) if creation_date_start.present? and creation_date_finish.present? == false
@bot_users = @bot_users.where('created_at < ?', creation_date_finish) if creation_date_start.present? == false and creation_date_finish.present?
@bot_users
end
end
不幸的是,这不会返回任何东西。我不确定参数部分,你能帮我吗?我将数据存储在数据库中并从对象中获取参数。
最佳答案
我和 Jaryl 在一起。
少if/elsif
.您要找的是case .由于您的许多查询具有相似的结构,因此您可以将它们填充到 else
中。 case
的条款声明。
少if x? == true
.如果x
有一个问号,那么它已经返回一个 true
或 false
.你不必说,if true == true
.就说,if x?
.喜欢,if value[0].present?
.根据您的具体要求,您可能可以跳过 present?
部分,以及。如果你只是想防范 nil
值,那么你可以做 if value[0]
.然而,正如 engineersmnky 在评论中指出的那样,如果你想防止空字符串、散列和数组——那么你需要坚持使用 if value[0].present?
。 .请记住,您可以随时粘贴 if
如果您不打算执行 else
,则在单行末尾声明.喜欢:
query_chain = query_chain.where('created_at > ?', value[0]) if value[0].present?
更少的类型转换(key.to_s
)。只需将键变量与另一个键进行比较。为什么要将其转换为字符串?
减少循环。特别是对于那些迭代变量和值比较 ( while $i < value.size
) - 讨厌!这:
while $i < value.size do
query_chain = query_chain.where("timezone = ?", value[$i].to_f)
$i += 1
end
不是惯用语。更好的做法是:
value.each do |timezone|
query_chain = query_chain.where("timezone = ?", timezone.to_f)
end
当然,您可以使该查询不那么冗长:
value.each do |timezone|
query_chain = query_chain.where(timezone: timezone.to_f)
end
但是,您在每个循环中所做的一切都是将时区转换为_f。那么,为什么不一次完成并链接一个查询,例如:
timezones = value.map{|timezone| timezone.to_f}
query_chain = query_chain.where(timezone: timezones)
当然,您可以省去临时变量赋值,然后执行以下操作:
query_chain = query_chain.where(timezone: value.map{|timezone| timezone.to_f})
如果您不介意排长队的话。
我喜欢 Jaryl 的方法。不过,如果您想坚持使用当前的方法,它可能类似于:
query.each do |hash|
hash.each_pair do |key, value|
if value
case key
when :timezone
query_chain = query_chain.where(timezone: value.map{|timezone| timezone.to_f})
when :created_at
query_chain = query_chain.where('created_at > ?', value[0]) if value[0]
query_chain = query_chain.where('created_at < ?', value[1].end_of_day) if value[1]
else
query_chain = query_chain.where(key => value)
end
end
end
end
这是 Jaryl 方法的一个稍微不同的实现...
class FilterQuery
attr_accessor :first_name,
:last_name,
:timezone,
:gender,
:locale,
:core_bot_id,
:source,
:creation_date_start,
:creation_date_finish
def initialize(params)
params.each{|k,v| send("#{k}=",v)}
end
def execute
return false unless valid?
@bot_users = BotUser.where(core_bot_id: core_bot_id)
[:first_name, :last_name].each do |var_sym|
val = send(var_sym)
@bot_users = @bot_users.where("#{var_sym} LIKE ?", "#{val}%") if val.present?
end
[:timezone, :locale, :gender, :source].each do |var_sym|
val = send(var_sym)
@bot_users = @bot_users.where(var_sym => val) if val.present?
end
@bot_users = @bot_users.where('created_at > ?', creation_date_start) if creation_date_start.present?
@bot_users = @bot_users.where('created_at < ?', creation_date_finish) if creation_date_finish.present?
@bot_users
end
private
def valid?
%w(male female).include? gender
end
end
关于ruby-on-rails - 带有或用于数组输入的查询链的 Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46488932/
我在为 MacOSX 构建的独立包中添加 DMG 背景的自定义图标时遇到问题。我在项目的根目录中添加了一个包。正在从中加载自定义图标,但没有加载 DMG 背景图标。我正在使用 Java fx 2.2.
Qt for Symbian 和 Qt for MeeGo 有什么区别?我知道 Qt 是一个交叉编译平台。这是否意味着如果我使用来自 Qt 的库,完全相同的库可以在所有支持 Qt 的设备(例如 Sym
我正在尝试使用 C# .NET 3.5/4.0 务实地运行 SQL Server 数据库的备份。我已经找到了如何完成此操作,但是我似乎找不到用于备份的命名空间库。 我正在寻找 Microsoft.Sq
我最近在疯狂学习 Java,但我通常是一名 .NET 开发人员。 (所以请原谅我的新手问题。) 在 .Net 中,我可以在不使用 IIS 的情况下开发 ASP.Net 页面,因为它有一个简化的 Web
这post仅当打印命令中有字符串时才有用。现在我有大量的源代码,其中包含一条声明,例如 print milk,butter 应该格式化为 print(milk,butter) 用\n 捕获行尾并不成功
所以我的问题是: https://gist.github.com/panSarin/4a221a0923927115584a 当我保存这个表格时,我收到了标题中的错误 NoMethodError (u
如何让 Html5 音频在点击时播放声音? (ogg 用于 Firefox 等浏览器,mp3 用于 chrome 等浏览器) 到目前为止,我可以通过 onclick 更改为单个文件类型,但我无法像在普
如果it1和it2有什么区别? std::set s; auto it1 = std::inserter(s, s.begin()); auto it2 = std::inserter(s, s.en
4.0.0 com.amkit myapp SpringMVCFirst
我目前使用 Eclipse 作为其他语言的 IDE,而且我习惯于不必离开 IDE 做任何事情 - 但是我真的很难为纯 ECMAScript-262 找到相同或类似的设置。 澄清一下,我不是在寻找 DO
我想将带有字符串数组的C# 结构发送到C++ 函数,该函数接受void * 作为c# 结构和char** 作为c# 结构字符串数组成员。 我能够将结构发送到 c++ 函数,但问题是,无法从 c++ 函
我正在使用动态创建的链接: 我想为f:param附加自定义转换器,以从#{name}等中删除空格。 但是f:param中没有转换器
是否可以利用Redis为.NET创建后写或直写式缓存?理想情况下,透明的高速缓存是由单个进程写入的,并且支持从数据库加载丢失的数据,并每隔一段时间持久保存脏块? 我已经搜查了好几个小时,也许是goog
我正在通过bash执行命令的ssh脚本。 FILENAMES=( "export_production_20200604.tgz" "export_production_log_2020060
我需要一个正则表达式来出现 0 到 7 个字母或 0 到 7 个数字。 例如:匹配:1234、asdbs 不匹配:123456789、absbsafsfsf、asf12 我尝试了([a-zA-Z]{0
我有一个用于会计期间的表格,该表格具有期间结束和开始的开始日期和结束日期。我使用此表来确定何时发生服务交易以及何时在查询中收集收入,例如... SELECT p.PeriodID, p.FiscalY
我很难为只接受字符或数字的 Laravel 构建正则表达式验证。它是这样的: 你好<-好的 123 <- 好的 你好123 <-不行 我现在的正则表达式是这样的:[A-Za-z]|[0-9]。 reg
您实际上会在 Repeater 上使用 OnItemDataBound 做什么? 最佳答案 “此事件为您提供在客户端显示数据项之前访问数据项的最后机会。引发此事件后,数据项将被清空,不再可用。” ~
我有一个 fragment 工作正常的项目,我正在使用 jeremyfeinstein 的 actionbarsherlock 和滑动菜单, 一切正常,但是当我想自定义左侧抽屉列表单元格时,出现异常
最近几天,我似乎平均分配时间在构建我的第一个应用程序和在这里发布问题!! 这是我的第一个应用程序,也是我们的设计师完成的第一个应用程序。我试图满足他所做的事情的外观和感觉,但我认为他没有做适当的事情。
我是一名优秀的程序员,十分优秀!