Ruby on rails 4 对复杂嵌套关联使用包含或左外部联接

Ruby on rails 4 对复杂嵌套关联使用包含或左外部联接,ruby-on-rails-4,activerecord,left-join,Ruby On Rails 4,Activerecord,Left Join,我有四种型号 class Company < ActiveRecord::Base has_many :share_types belongs_to :user end class ShareType < ActiveRecord::Base has_many :shares belongs_to :company end class Share < ActiveRecord::Base belongs_to :user belongs_to :shar

我有四种型号

class Company < ActiveRecord::Base
  has_many :share_types
  belongs_to :user
end
class ShareType < ActiveRecord::Base
  has_many :shares
  belongs_to :company
end
class Share < ActiveRecord::Base
  belongs_to :user
  belongs_to :share_type
end
class User < ActiveRecord:Base
 has_many :companies
 has_many :shares
end
但与左外连接另一个我不知道如何使用包含或条件另一个提示是

Company.includes(share_types:[:shares]).where(shares:{user_id: @user.id} OR companies:{user_id: 1})

我怎样才能做到这一点。

在参考资料的帮助下,我能够得到预期的结果。这是我的问题,只是为了帮助别人

Company.includes(share_types:[:shares]).where("shares.user_id=? OR companies.user_id=?", 1,1).references(:shares)
多亏了欧比·费尔南德斯的Rails 4 Way,它才能够工作

Company.includes(share_types:[:shares]).where("shares.user_id=? OR companies.user_id=?", 1,1).references(:shares)