2013-08-16 1 views
0

: through 조건에없는 레코드를 가져와야합니다.: active 조건에없는 activerecord 레코드

나는 3 개 테이블이 : 나는 모든 A가 listOfA = B.A하여 특정 B에 연결 얻을

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 

을하지만 난 어떻게 내가 할 수있는 B에 모두를 얻지도해야합니까?

답변

0

좋아요, 내 가입 쿼리에 넣을 두 조건이 있습니다. 내 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 }) 

그리고 내 모든하지 BA에이

관련 문제