Rails,如何使用has_many合并两个约束:through

时间:2017-05-21 12:01:23

标签: ruby-on-rails activerecord

我希望将附件范围限制为尚未分配给评论的current_user附件或附件,以便稍后find

附件has_one :comment, optional: true

附件has_one :user, through :comment,评论 - 通过任务,任务 - 通过项目

> user.attachments.to_sql
=> "SELECT \"attachments\".* FROM \"attachments\" INNER JOIN \"comments\" ON \"attachments\".\"comment_id\" = \"comments\".\"id\" INNER JOIN \"tasks\" ON \"comments\".\"task_id\" = \"tasks\".\"id\" INNER JOIN \"projects\" ON \"tasks\".\"project_id\" = \"projects\".\"id\" WHERE \"projects\".\"user_id\" = $1 ORDER BY \"comments\".\"created_at\" ASC, tasks.position"

如何使其发挥作用

> user.attachments.or(Attachment.where(comment_id: nil))
Relation passed to #or must be structurally compatible. Incompatible values: [:order, :joins, :references]

我试过了

  without_comment = Attachment.joins(comment: [task: [:project]]).where(comment_id: nil).order('"comments"."created_at" ASC, tasks.position')
  user.attachments.or(without_comment)

[35] pry(#<AttachmentResource>)> without_comment.to_sql
=> "SELECT \"attachments\".* FROM \"attachments\" INNER JOIN \"comments\" ON \"comments\".\"id\" = \"attachments\".\"comment_id\" INNER JOIN \"tasks\" ON \"tasks\".\"id\" = \"comments\".\"task_id\" INNER JOIN \"projects\" ON \"projects\".\"id\" = \"tasks\".\"project_id\" WHERE \"attachments\".\"comment_id\" IS NULL ORDER BY \"comments\".\"created_at\" ASC, tasks.position"
[36] pry(#<AttachmentResource>)> user.attachments.or(without_comment)
ArgumentError: Relation passed to #or must be structurally compatible. Incompatible values: [:order, :joins, :references]
from /home/bjorn/.rvm/gems/ruby-2.4.1@ngrx-todolist/gems/activerecord-5.1.1/lib/active_record/relation/query_methods.rb:655:in `or!'

我想找到所有current_user附件以及没有所有者的附件 - &gt;没有家长评论

查询Attachment.joins(comment: [task: [:project]]).where('projects.user_id = ? OR attachments.comment_id IS NULL', user.id)不包括没有评论的附件,为什么?

[10] pry(#<CommentResource>)> Attachment.all
=> [#<Attachment:0x00000008a6f3a8
  id: 1,
  file: "attachments.rb",
  comment_id: 1,
  created_at: Sun, 21 May 2017 14:18:21 UTC +00:00,
  updated_at: Sun, 21 May 2017 14:18:21 UTC +00:00>,
 #<Attachment:0x00000008a6f268
  id: 2,
  file: "attachments.rb",
  comment_id: 1,
  created_at: Sun, 21 May 2017 14:18:21 UTC +00:00,
  updated_at: Sun, 21 May 2017 14:18:21 UTC +00:00>,
 #<Attachment:0x00000008a6f128
  id: 3,
  file: "attachments.rb",
  comment_id: nil,
  created_at: Sun, 21 May 2017 14:29:51 UTC +00:00,
  updated_at: Sun, 21 May 2017 14:29:51 UTC +00:00>]
[11] pry(#<CommentResource>)> Attachment.joins(comment: [task: [:project]]).where('projects.user_id = ? OR attachments.comment_id IS NULL', user.id).to_sql
=> "SELECT \"attachments\".* FROM \"attachments\" INNER JOIN \"comments\" ON \"comments\".\"id\" = \"attachments\".\"comment_id\" INNER JOIN \"tasks\" ON \"tasks\".\"id\" = \"comments\".\"task_id\" INNER JOIN \"projects\" ON \"projects\".\"id\" = \"tasks\".\"project_id\" WHERE (projects.user_id = 1 OR attachments.comment_id IS NULL)"
[12] pry(#<CommentResource>)> Attachment.joins(comment: [task: [:project]]).where('projects.user_id = ? OR attachments.comment_id IS NULL', user.id)
=> [#<Attachment:0x0000000891c7a8
  id: 1,
  file: "attachments.rb",
  comment_id: 1,
  created_at: Sun, 21 May 2017 14:18:21 UTC +00:00,
  updated_at: Sun, 21 May 2017 14:18:21 UTC +00:00>,
 #<Attachment:0x0000000891c668
  id: 2,
  file: "attachments.rb",
  comment_id: 1,
  created_at: Sun, 21 May 2017 14:18:21 UTC +00:00,
  updated_at: Sun, 21 May 2017 14:18:21 UTC +00:00>]

1 个答案:

答案 0 :(得分:0)

感谢@fanta

<script type="text/javascript">
  function ShowHideDiv(Fish) {
    var subAnimal = document.getElementById("subFish");
    subFish.style.display = Fish.checked ? "block" : "none";
    console.log(Fish.value)
    console.log("Text Inside LABEL:" + Fish.parentNode.textContent)
  }
</script>

<label for="Fish">
    <input type="checkbox" id="Fish" onclick="ShowHideDiv(this)" value="Fish"/> Fish 
</label>

<div id="subFish" style="display: none">

  <label> 
    <input type="checkbox" id="subFish1" /> Tank 
</label>

  <label> 
    <p id="subFishs1" ></p> 
</label>

  <label>
	<input type="checkbox" id="subFish2" /> Fishing 
</label>

  <label> 
    <p id="subFishs2" ></p> 
</label>

  <script>
    function ShowHideDiv2(this) {

      var a = document.getElementById("subAnimal1").checked;
      var b = document.getElementById("subAnimal2").checked;

      if (a && b) {
        document.getElementById("subAnimal1").addEventListener("click", function() {
              document.getElementById("subAnimals1").innerHTML = "Bad";
            } else if (a && !b) {
              document.getElementById("subAnimal2").addEventListener("click", function() {
                  document.getElementById("subAnimals2").innerHTML = "Cute";
                } else {
                  alert("Under Development!");
                }
              }
  </script>