【问题标题】:Rails way: Finding a deeply nested association with many where clauseRails 方式:找到与许多 where 子句的深层嵌套关联
【发布时间】:2023-04-01 02:23:01
【问题描述】:

这似乎是一个简单的问题,但对我来说有点困惑。我想找到一个处理这个问题的好方法。 我有5个模型。

工作

class Job < ActiveRecord::Base
  has_many :job_entries_jobs
  has_many :job_entries, through: :job_entries_jobs
end

求职

class JobEntry < ActiveRecord::Base
    has_many :attachments
    has_many :job_entries_jobs
    has_many :pinned_entries, through: :job_entries_jobs
    has_many :jobs, through: :job_entries_jobs
    belongs_to :user, class_name: 'User', foreign_key: 'user_id'
end

JobEntriesJob

class JobEntriesJob < ActiveRecord::Base
  self.table_name = 'job_entries_jobs'
  has_many :pinned_entries
  belongs_to :job_entry
  belongs_to :job, touch: true
end

PinnedEntry

class PinnedEntry < ActiveRecord::Base
  self.table_name = "pinned_entries"
  belongs_to :job_entries_job
  has_and_belongs_to_many :users
end

用户

class User < ActiveRecord::Base
  has_many :job_entries
  has_many :pinned_entries
end

我可以通过associationsrails 方式使用job_iduser_id(job_id 和user_id 从request 参数获取)获得pinned_entries(我可以使用SQL 获得pinned_entries)。如果是,我应该把这个code 放在哪里?

如果我有一个attachment 模型怎么样

class Attachment < ActiveRecord::Base
    self.table_name = 'attachments'

    belongs_to :job_entry, :class_name => 'JobEntry', :foreign_key => 'job_entry_id'

end

我想includeattachment(链接和文件名)和JobEntrycontent)? 这是应该的输出

{
  "listPinnedEntries": [
    {
      "id": 1
      "user_id": 
      "text": "content of entry",
      "user": {
        "id": 
        "username": "jamesnguyen"
      },
      "attachments": [
        {
          "id": 
          "job_entry_id":
          "comment": "file name",
          "file": {
            "url": "link here"
          }
        },
        {
         "id": 
          "job_entry_id":
          "comment": "file name",
          "file": {
            "url": "link here"
          }
        }
      ]
    }]
}

感谢您的帮助

【问题讨论】:

  • 尝试在您的视图中执行user_id.pinned_entries,这将查看与该特定user 相关的所有条目。
  • 感谢您的帮助。我已经稍微更新了这个问题。你能再读一遍吗?

标签: ruby-on-rails ruby-on-rails-4 activerecord associations rails-activerecord


【解决方案1】:

给定job_iduser_id

user = User.find(user_id)
pinned_entries = user.pinned_entries.includes(:job_entries_job).where(job_entries_jobs: {job_id: job_id})

更新(关于附件的第二个问题):

由于您正在生成值的哈希值,因此修改 PinnedEntry 模型(或者甚至可能是 @Robin 回答的 Job 模型和 User 模型)比进行脏循环更容易:

class PinnedEntry < ActiveRecord::Base
  self.table_name = "pinned_entries"
  belongs_to :job_entries_job
  belongs_to :job_entry, through: :job_entries_job
  has_many :attachments, through: :job_entry
  has_and_belongs_to_many :users
end

然后,从我上面的第一个答案继续:

pinned_entries.as_json(
  only: [:id, :user_id, :text],
  include: [
    user: {
      only: [:id, :username]
    },
    attachments: {
      only: [:id, :job_entry_id, :comment]
    }
  ]
)

这将返回一个与您想要的值相同的哈希值。注意:我没有将file: url 包含在您的附件模型中,没有belongs_to :file 或可以帮助我确定附件模型与fileurl 的关系的东西。如果它不是一个属性,那么我认为您必须手动将这些添加到生成的哈希中。

【讨论】:

  • 感谢您的帮助。如果我有一个attachment 模型并且想要包含,该怎么办?
  • @JamesNguyen,您能否详细说明您关于附件模型的第二个问题?这第二个问题是否仍然与第一个问题中的 user_id 和 job_id 相关,或者这是一个单独的问题?我的意思是你想检索链接 == 'something && file_name == 'somefilename' && 与附件关联的 job_entry 具有内容 == 'somecontent' 的所有附件?
  • 我想include关联信息,减少查询。我已经把输出应该是
【解决方案2】:

向模型JobUser 添加一个额外的has_many 并通过:through 将其与您的PinnedEntry 链接可能是个好主意。

您的 Job 模型将更改为:

class Job < ActiveRecord::Base
  has_many :job_entries_jobs
  has_many :job_entries, through: :job_entries_jobs
  has_many :pinned_entries: through: :job_entries
end

然后您可以像往常一样在Job 记录上使用.pinned_entries。因此,您更改了 User 模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 2017-04-02
    • 1970-01-01
    相关资源
    最近更新 更多