0

我有这些联想

class Incident < Activerecord::Base
   belongs_to :building

class Building < Activerecord::Base
   has_many :incidents
   belongs_to :contact

class Contact < Activerecord::Base
    has_many :buildings    
    belongs_to :company

class Company < Activerecord::Base
    has_many :contacts

我想对事件进行关键字搜索。此搜索还应查找与事件相关的公司。

因此,我创建了这个方法

def self.filtered
      includes(:building => :contact => :company).where("lower(unaccent(incidents.description)) ilike lower(unaccent('%#{q}%')) or lower(unaccent(companies.name)) ilike lower(unaccent('%#{q}%'))")
end  

不幸的是,这不起作用。

ActiveRecord::ConfigurationError - Association named 'company' was not found; perhaps you misspelled it?

我不知道为什么,但是“包含”方法可以有两个级别的关联,但不能有三个级别。

我怎么解决这个问题?

谢谢

4

0 回答 0