2017-05-02 34 views
0

私はRails 5を使っていますが、私は他のクエリとhas_and_belongs_to_many関係をしたいと考えています。 2つのクエリをマージするのではなく、同じではありません。実際に私はこのきれいな方法をしたい。Rails has_and_belongs_to_many他のクエリとの関連

私はそれをきれいにすることができますか?

@branch.course_contents.active + CourseContent.show_all_branches 

このSQL出力をレールで書きたいと思います。そのsqlコマンドは私のためにうまく動作します。

select *, 
    (select branch_id 
    from branches_course_contents 
    where course_content_id=course_contents.id and branch_id=2) as Sube 
from course_contents 
where 
    (select branch_id 
    from branches_course_contents 
    where course_content_id=course_contents.id and branch_id=2)=2 
    or show_all_branches=true) 
    and content_type=0 

答えて

0

これを試してみてください:ありがとう

@branch.course_contents.active.merge(CourseContent.show_all_branches) 
+0

が、その私には働いていません。 –

+0

「働いていない」とはどういう意味ですか?コンソールからクエリを表示できますか? – archana

+0

質問の説明が更新されました。そのsqlコマンドは正常に動作します。私はレールでそれを書いています。 –

関連する問題