Rails搜索几张桌子

时间:2018-11-05 19:52:41

标签: sql ruby-on-rails postgresql

我要按多个表进行搜索

我的模式是

fields => :id

class Manufacturer 
 has_many :brands, dependent: :destroy
 has_many :translations, class_name: 'ManufacturerTranslation', dependent: :destroy    
end

fields => :id,:manufacturer_id, :locale ,:name

class ManufacturerTranslation 
  belongs_to :manufacturer
end

fields => :id, :manufacturer_id

class Brand
  belongs_to :manufacturer, inverse_of: :brands
  has_many :translations, class_name: 'BrandTranslation', dependent: :destroy
end 

fields => :id,:brand_id, :locale ,:name

class BrandTranslation 
  belongs_to :brand
end

我必须搜索BrandTranslation名称和/或ManufacturerTranslation名称

我尝试过

joins(:translations,:manufacturer_translations).
    where('brand_translations.name ILIKE  ? or manufacturer_translations.name = ?"', "%#{query}%", "%#{query}%")

如何编写适当的sql或activerecord查询?

0 个答案:

没有答案