Rails虚拟属性搜索或SQL组合列搜索

11

我有一个用户模型,其中有'first'和'last'属性

所以例如 User.first.first #=> "Charlie" User.first.last #=> "Brown"

这个用户模型还有一个虚拟属性'full_name'

#user.rb
def full_name
  [first,last].join(' ')
end

def full_name=(name) #don't know what to do with people w/ middle names
  split = name.split(' ')
  self.first = split[0]
  self.last = split[1]
end

所以举个例子:
User.first.full_name = "Charlie Brown" #=> "Charlie Brown"
User.first.full_name = "Homer Simpson" #=> "Home Simpson"
User.first.save
User.first.first #=> "Homer"
User.first.last #=> "Simpson"

希望能够通过虚拟属性进行搜索,例如动态查找:
User.find_by_full_name('Home Simpson') # this doesn't work

在查找中使用条件的示例:

User.all(:conditions => ['full_name LIKE ?', query]) #this doesn't work

我希望能在SQL语言中找到至少一些可以做到这一点的方法;如果有动态虚拟属性查找,那么在 strudel 上会有额外的香草源。(任何人在今年冬天有这个吗?)

我还担心搜索一个名字,例如,“Holmes”只能在“first”列中搜索而不能在“last”中检索,例如,User.first.full_name #=> "Sherlock Holmes"

我尝试进行更全面的搜索:

user.rb

def self.find_by_full_name(name) #returns an array of User model
  return all if name.blank?
    
  split = name.split(' ', 2)
  output = []
  if split.length > 1
    with_scope( :find => { :conditions => ['first LIKE ?', "%#{split[0]}%"] }) do
      output << all(:conditions => ['last LIKE ?', "%#{split[1]}%"])
      output.flatten!
    end
  elsif split.length == 1
    output << all(:conditions => ['first LIKE ?', "%#{split[0]}%"])
    output << all(:conditions => ['last LIKE ?', "%#{split[0]}%"])
    output.flatten!
  end
end

例如
User.find_by_full_name("John").map(&:full_name) #=> ["John Resig", "John Doe"]
User.find_by_full_name("Doe").map(&:full_name) #=> ["John Doe", "Philips Doeringer"]
User.find_by_full_name("John Doe").map(&:full_name) #=> ["John Doe"]

但我认为这里的find_by_full_name方法有点笨重。

我的意思是,如果我有一个名为full_name的列,每次都会通过一个after save过滤器设置为first和last的组合。因此,查找人的姓名,特别是对于这个人的模糊记忆,是很有帮助的。所以,如果我记得这个人的名字中有'Doe',我可以简单地使用User.find_by_full_name('Doe')返回尽可能多的结果以进一步锁定它。

而且,由于它是一个列,如果我必须做像Project.find(:all,:include => :users, :conditions=>['users.full_name LIKE ?', query])这样的事情,我可以在find(:conditions[...])子句中搜索它。

#project.rb
has_many :assignments
has_many :users, :through=>:assignments

#user.rb
has_many :assignments
has_many :projects, :through => :assignments

#assignment.rb
belongs_to :user
belongs_to :project

节日快乐

2个回答

11

你可以在 user.rb 中使用命名作用域(named_scope):

named_scope :find_by_full_name, lambda {|full_name| 
  {:conditions => {:first => full_name.split(' ').first, 
     :last => full_name.split(' ').last}}
}

那么你可以执行 User.find_by_full_name('John Carver')

响应需求变化的新内容

named_scope :find_by_full_name, lambda {|full_name| 
  {:conditions => ["first LIKE '%?%' or last LIKE '%?%'", 
    full_name.split(' ').first, full_name.split(' ').last]}}

我明白了。我猜当我用这种方式搜索全名时,它确实有效。-- 但还是谢谢你的回答。最好的祝福,N - Nik So
我明白了。对不起,我改了它。 - Nik So

0

我也觉得Jim的回答很有帮助。谢谢。不过我会稍微改动一下。 这段代码会导致你失去任何中间名字。我下面的代码有点乱,但可以保留中间名和复合姓(比如Jean-Claude van Damme)。第一个名字后面的所有内容都放在last_name字段中。

named_scope :find_by_full_name, lambda { |full_name| 
 {:conditions => {:first_name => full_name.split(' ').first, 
   :last_name => full_name.split(' ')[1, full_name.split(' ').length-1].join(' ')}
   }
}

当然,如果有更好的方法来完成这个任务是非常欢迎的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接