【问题标题】:Ruby on Rails: how to use find on multiple conditions and multiple tables?Ruby on Rails:如何在多个条件和多个表上使用 find?
【发布时间】:2013-04-01 23:24:23
【问题描述】:

我有一个产品模型和一个类别模型。产品 has_many_and_belongs_to 类别,反之亦然。现在我想使用 Product.find 来搜索 Product.name 和 Product.categories。我尝试了类似的方法,但没有成功:

Product.find(:all, :conditions => ['name LIKE ? or categories LIKE ?', '%#{keyword}%']

基本上,我正在尝试获取名称为“汽车”或类别为“汽车”的产品的结果。提前致谢!

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:
    Product.joins(:categories).where('products.name LIKE ? or categories.name LIKE ?', "%#{keyword}")
    

    如果您使用 rails > 3,我认为这样的内容可以帮助您

    【讨论】:

      猜你喜欢
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2010-11-01
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多