Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 在rails中修改简单搜索以搜索链接数据?_Mysql_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Mysql 在rails中修改简单搜索以搜索链接数据?

Mysql 在rails中修改简单搜索以搜索链接数据?,mysql,ruby-on-rails,ruby,ruby-on-rails-3,Mysql,Ruby On Rails,Ruby,Ruby On Rails 3,我遵循:(尽管是更新版本)在我的应用程序上实现搜索 现在搜索时,它会搜索1个表。如果该表链接到另一个表或联接表,该怎么办?有没有办法让它也搜索这些字段 我这样说是因为目前我让它从一个表中搜索字段: def self.search(search) if search where('LOWER (description) LIKE ? OR LOWER (title) LIKE ?', "%#{search}%" , "%#{search}%") else s

我遵循:(尽管是更新版本)在我的应用程序上实现搜索

现在搜索时,它会搜索1个表。如果该表链接到另一个表或联接表,该怎么办?有没有办法让它也搜索这些字段

我这样说是因为目前我让它从一个表中搜索字段:

def self.search(search)
    if search
      where('LOWER (description) LIKE ? OR LOWER (title) LIKE ?', "%#{search}%" , "%#{search}%")
    else
      scoped
    end
  end

但我真的希望它也能搜索链接表。有什么想法吗?

是的,您可以将联接添加到查找中

# models/project.rb
def self.search(search)
  if search
    find(:all, :joins => :other_model, :conditions => ['projects.name LIKE :search or other_models.name LIKE :search', {:search => "%#{search}%"}])
  else
    find(:all)
  end
end
好的,这将是未来更好的例子

  def self.search(search)
      if search
        joins(:other_model).where('LOWER (projects.description) LIKE ? or LOWER (other_models.name) LIKE ?', "%#{search}%", "%#{search}%")
      else
        scoped
      end
    end

您可以看看searchlogic(它在github上有一个rails 3分支),它使搜索变得更加容易

是的,它是。。。尽管这是一篇关于未来的有趣文章,但对于Rails3:Client.joins(:orders.where('orders.created_at'=>time_range)来说,这确实是一种很好的语法。结帐:谢谢,蒂姆!今晚我会试试这个,我会告诉你进展如何!