Ruby on rails 对象数组的类SQL语法
我有一个对象数组。我需要使用类似SQL的条件,其中类似“%value%”的字段用于此数组中的某些对象字段。 怎么做Ruby on rails 对象数组的类SQL语法,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个对象数组。我需要使用类似SQL的条件,其中类似“%value%”的字段用于此数组中的某些对象字段。 怎么做 arr = %w[hello quick bool boo foo] arr.select { |x| x.include?("foo") } => ["bool", "boo", "foo"] 编辑: 例如,我有一个包含用户的数组,我需要找到所有名为ike和电子邮件为123的用户 编辑2: 我需要一种方法从我的用户数组中获取名为smth和电子邮件为smth的用户。用户有名
arr = %w[hello quick bool boo foo]
arr.select { |x| x.include?("foo") }
=> ["bool", "boo", "foo"]
编辑:
例如,我有一个包含用户的数组,我需要找到所有名为ike和电子邮件为123的用户
编辑2:
我需要一种方法从我的用户数组中获取名为smth和电子邮件为smth的用户。用户有名字和电子邮件
编辑3:
我的所有用户都在数据库中。但是我有一些业务逻辑,在这个逻辑的末尾,我有一个用户数组。接下来我需要用一些文本过滤这个数组:ike代表名字,123代表电子邮件。怎么做
arr = %w[hello quick bool boo foo]
arr.select { |x| x.include?("foo") }
=> ["bool", "boo", "foo"]
或者,在您的情况下,如果您有一个对象数组,则可以执行以下操作:
x.first_name.include?("foo") && x.email.include?("123")
为了进行更多的定制,如果您可以使用ruby方法来实现以下功能,则可以将Arrayselect与s一起使用:
User = Struct.new(:email, :first_name) # Just creating a cheap User class here
users = [
User.new('1@a.com' , 'ike'),
User.new('123@a.com', 'bob'),
User.new('123@a.com', 'ike'),
]
# results will be an array holding only the last element in users
results = users.find_all do |user|
user.email =~ /123/ and
user.first_name =~ /ike/
end
User = Struct.new(:email, :first_name) # Just creating a cheap User class here
users = [
User.new('1@a.com' , 'ike'),
User.new('123@a.com', 'bob'),
User.new('123@a.com', 'ike'),
]
def where(array, sql)
sql = sql.gsub(/\s+AND\s+/, ' ') # remove AND's
terms = Hash[ *sql.split(/\s+LIKE\s+| /) ] # turn "a LIKE 'b'" into {'a': "'b'"}
array.find_all do |item|
terms.all? do |attribute, matcher|
matcher = matcher.gsub('%', '.*') # convert %
matcher = matcher.gsub(/^['"]|["']$/, '') # strip quotes
item.send(attribute) =~ /^#{matcher}$/
end
end
end
# results will be an array holding only the last element in users
results = where(users, "first_name LIKE '%ike%' AND email LIKE '%123%'")
编写自己的sql解析器似乎是一个非常糟糕的主意,但如果您真的需要解析简单的sql where子句,您可以这样做:
User = Struct.new(:email, :first_name) # Just creating a cheap User class here
users = [
User.new('1@a.com' , 'ike'),
User.new('123@a.com', 'bob'),
User.new('123@a.com', 'ike'),
]
# results will be an array holding only the last element in users
results = users.find_all do |user|
user.email =~ /123/ and
user.first_name =~ /ike/
end
User = Struct.new(:email, :first_name) # Just creating a cheap User class here
users = [
User.new('1@a.com' , 'ike'),
User.new('123@a.com', 'bob'),
User.new('123@a.com', 'ike'),
]
def where(array, sql)
sql = sql.gsub(/\s+AND\s+/, ' ') # remove AND's
terms = Hash[ *sql.split(/\s+LIKE\s+| /) ] # turn "a LIKE 'b'" into {'a': "'b'"}
array.find_all do |item|
terms.all? do |attribute, matcher|
matcher = matcher.gsub('%', '.*') # convert %
matcher = matcher.gsub(/^['"]|["']$/, '') # strip quotes
item.send(attribute) =~ /^#{matcher}$/
end
end
end
# results will be an array holding only the last element in users
results = where(users, "first_name LIKE '%ike%' AND email LIKE '%123%'")
这只适用于where子句,其中只包含由AND连接的LIKE语句。添加对所有有效SQL的支持只是读者的一个练习,或者更好的是,就这样吧。我构建了一个ruby gem,为您可能想要尝试的哈希或对象数组启用类似SQL的语法
require 'uber_array'
# Array of Hash elements with strings as keys
items = [
{ 'name' => 'Jack', 'score' => 999, 'active' => false },
{ 'name' => 'Jake', 'score' => 888, 'active' => true },
{ 'name' => 'John', 'score' => 777, 'active' => true }
]
uber_items = UberArray.new(items)
uber_items.where('name' => 'John')
uber_items.where('name' => /Ja/i)
uber_items.like('ja')
uber_items.where('name' => %w(Dave John Tom))
uber_items.where('score' => 999)
uber_items.where('score' => ->(s){s > 900})
uber_items.where('active' => true, 'score' => 800..900)
你从哪里得到的数组?数组没有查询语言,它们只是容器。数据库通常有SQL,如果SQL是您的数据源,那么您应该在SQL中应用查询。你可以把你的数组放到一个像SQLite这样的数据库中,然后使用gem Sequel查询它,这是一个很好的ORM,而且很简单。我们需要更多信息。描述对象的属性。显然,如果你想通过他们过滤,他们必须有名字和电子邮件,那么为什么你不能用普通的Ruby方法来询问他们自己呢?我已经编辑了我的问题。你有一个数据库。接下来我需要用一些文本过滤这个数组:ike代表名字,123代表电子邮件。在请求用户时在数据库中执行此操作,然后再执行其他操作。这就是SQL查询所做的,它过滤以检索整个数据库的子集,并将其作为数组返回到Ruby代码中。听起来你不明白我们是如何使用Ruby这样的语言的数据库的。你有没有看过这个关于Rails查询中like子句的正确格式的问题?