Ruby on rails Rails方式:查找与多个where子句的深度嵌套关联
这似乎是一个简单的问题,但对我来说有点困惑。我想找到一个好办法来处理这件事。 我有5个模型 工作Ruby on rails Rails方式:查找与多个where子句的深度嵌套关联,ruby-on-rails,ruby-on-rails-4,activerecord,associations,rails-activerecord,Ruby On Rails,Ruby On Rails 4,Activerecord,Associations,Rails Activerecord,这似乎是一个简单的问题,但对我来说有点困惑。我想找到一个好办法来处理这件事。 我有5个模型 工作 我想包括附件(链接和文件名)和作业条目(内容)? 下面是应该显示的输出 { "listPinnedEntries": [ { "id": 1 "user_id": "text": "content of entry", "user": { "id": "username": "jamesnguyen"
我想
包括
附件
(链接和文件名)和作业条目
(内容
)?
下面是应该显示的输出
{
"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"
}
}
]
}]
}
感谢您提供的
作业id
和用户id
的帮助:
user = User.find(user_id)
pinned_entries = user.pinned_entries.includes(:job_entries_job).where(job_entries_jobs: {job_id: job_id})
更新(关于附件的第二个问题):
由于您正在生成一个散列值,因此修改您的PindedEntry模型(甚至可能是@Robin回答的作业模型和用户模型)将比执行脏循环更容易:
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
这将返回与所需值相同的散列值。注意:我没有包括附件模型中的
文件:url
,没有属于
或帮助我确定附件模型与文件和url
之间关系的内容。如果它不是一个属性,那么我认为您必须手动将它们添加到生成的哈希中。最好在models作业
和用户
中添加一个附加属性,并通过
您的作业
型号将更改为:
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
类作业
然后您可以像往常一样在作业
记录上使用.pinted_条目
。因此,您可以更改您的用户
模型。尝试在视图中执行用户id。固定的\u条目
,它将查看与特定用户
相关的所有条目。感谢您的帮助。我已经更新了一点这个问题。你能再读一遍吗?谢谢你的帮助。如果我有一个附件
模型,并且想要包括在内,怎么样?@JamesNguyen,你能详细谈谈关于附件模型的第二个问题吗?第二个问题是否仍然与第一个问题中的用户id和工作id相关,或者这是一个单独的问题?我的意思是,您希望检索所有附件,其中link='something&&file\u name='somefilename'&&与附件关联的作业条目具有content='somecontent'?我希望包含关联信息并减少查询。我已经把应该是
class User < ActiveRecord::Base
has_many :job_entries
has_many :pinned_entries
end
class Attachment < ActiveRecord::Base
self.table_name = 'attachments'
belongs_to :job_entry, :class_name => 'JobEntry', :foreign_key => 'job_entry_id'
end
{
"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 = User.find(user_id)
pinned_entries = user.pinned_entries.includes(:job_entries_job).where(job_entries_jobs: {job_id: job_id})
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]
}
]
)
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