Class User < ActiveRecord::Base
has_one :child
end
Class Child < ActiveRecord::Base
belongs_to :user
end
例えば上記のような関係だったとして、
childモデルのレコードが存在しているusersレコード全て、
childモデルのレコードが存在していないusersレコード全て、
を抽出する方法を教えて下さい。
よろしくお願いします。
こんなページがありました。
http://qiita.com/abey1192/items/198f3c15d14392023a63
http://labs.timedia.co.jp/2013/10/activerecord4sql-arel.html
User の id と、Child の user_id が紐づいているとして、こんな感じになるのじゃないかと思います。
user_table = User.arel_table child_table = Child.arel_table condition = child_table[:user_id].eq(user_table[:id]) users__has_child = User.where(UserGroup.where(condition).exists).all users__no_child = User.where(UserGroup.where(condition).exists.not).all