您好,我的查詢存在問題。 有我的模型如下:當有連接時,Active Record不會更新集合並且包含在查詢中
class Owner
has_many :busiensses
has_many :adverts
end
class Business
belongs_to :owner
end
class Advert
belongs_to :owner
end
當我做這個查詢一切正常,並返回正確的集合全所需要的對象:
Owner.joins(:adverts).includes(:businesses)
.where(businesses: {owner_id: nil})
但當我添加到查詢更新它會引發錯誤
Owner.joins(:adverts).includes(:businesses)
.where(businesses: {owner_id: nil})
.update_all(status: 'sth')
錯誤:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "businesses"
問題在哪裏?我敢打賭,這個錯誤從SQL和它引發時,你忘記添加......到從聲明和sth需要在進一步的條件,但在AR我忘了補充它?
嘗試做'.update_all(「owners.status =‘某事’」)' – Syl