【问题标题】:activerecord records not in the :through conditionactiverecord 记录不在 :through 条件下
【发布时间】:2013-08-16 10:23:14
【问题描述】:

我需要获取不在 :through 条件下的记录。

我有 3 张桌子:

class A < ActiveRecord::Base
  has_many :B_A
  has_many :B, through: :B_A
end
class B < ActiveRecord::Base
  has_many :B_A
  has_many :A, through: :B_A
end
class BA < ActiveRecord::Base
  belongs_to :A
  belongs_to :B
end

我通过listOfA = B.A 将所有 A 链接到特定 B,但我还需要让所有 A 不在任何 B 中。我该怎么做?

【问题讨论】:

    标签: activerecord


    【解决方案1】:

    好的,我有两个条件可以放入我的联接查询中。 我的 MySql 是:

    select a.`id`, a.`label`
    from `A` a
    left join `BA` ba
    on a.`id` = ba.`a`
    where a.`uid`='userId'
    and ba.`a` is null
    

    我现在的活动记录是:

    A.select(['A.id', :label])
      .joins('left outer join B_A on B_A.a_id = A.id')
      .where({ user_id: current_user.id, "B_A.a_id" => nil })
    

    我所有的 A 都不在 BA

    【讨论】:

      猜你喜欢
      • 2020-06-20
      • 2011-10-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-05-07
      • 2011-08-19
      相关资源
      最近更新 更多