2014-01-21 121 views
0

我有一个名为“image”的模型,我需要找到所有图像,但图像名称属性中包含表达式“User”的图像除外。在Rails 4中查找带有排除项的所有条目

Image.rb:

class Image < ActiveRecord::Base 
    attr_accessible :name 
end 

所以,它应该是这样的:

@images = Image.where( “!NAME =〜 '/用户/我'”)

正确的术语是什么?

+0

通常你不会在SQL中使用正则表达式,肯定在你的情况有没有必要。一个简单的'NOT LIKE'子句就足够了。 – thomasfedb

回答

1

的PostgreSQL在Rails 4中:

Image.where.not("name ~ ?", "User") 

MySQL in Rails 4:

Image.where.not("name RLIKE ?", "User") 
+0

来源:http://viget.com/extend/regular-expressions-in-mysql – 2014-01-21 18:18:55

0
@images = Image.where.not(name: user) 

或者,如果 “用户” 是字符串:

@images = Image.where.not(name: "user") 
+0

好的,但它应该是一个REGEXP,所以:name不应该包含单词“user”。例如。 “第一个用户”应该是一个结果... – user929062

0

尝试这个

@images = Image.where.not( “name = '用户'”)

+0

我也在下面添加了commoon:它应该是一个REGEXP,所以::name不应该包含单词“user”。例如。 “第一个用户”不应该是一个结果......你的表达式排除了所有的图像,其中:name =='user'我猜。 – user929062

相关问题