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

find 中的条件示例:

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

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

我还担心正在搜索的名称,例如,“Holmes”可能只能在“第一”列中搜索,但不能在“最后”列中搜索,例如,User.first.full_name #=> "Sherlock Holmes"

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

用户.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 列,它每次都由一个后保存过滤器设置,并带有 first 和 last 的 concat。所以找到一个人的名字,尤其是对这个人的模糊记忆,是有帮助的。因此,如果我记得那个人的名字或姓氏中的“Doe”,我总是可以做一个简单的 User.find_by_full_name('Doe') 来尽可能多地返回以进一步确定它。

And since it is a column, I can search it in a find(:conditions[...]) clause if I have to do something like Project.find(:all,:include => :users, :conditions=>['users.full_name LIKE ?', query]) where

#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

Happy Holidays N

4

2 回答 2

11

You can used a named_scope in your user.rb:

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

Then you can do User.find_by_full_name('John Carver')

new stuff in response to changes in requirement

named_scope :find_by_full_name, lambda {|full_name| 
  {:conditions => ["first LIKE '%?%' or last LIKE '%?%'", 
    full_name.split(' ').first, full_name.split(' ').last]}}
于 2009-11-27T02:39:47.210 回答
0

I found Jim's answer helpful as well. Thanks. I'd make a slight change though. This current code causes you to loose any middle names. What I have below is a bit messy but preserves middle names and compound last names (think Jean-Claude van Damme). Everything after the first name goes in the last_name field.

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(' ')}
   }
}

Of course any cleaner way to do this is welcome.

于 2011-01-10T06:17:51.557 回答