Rails - 如何在多对多关系中列出每个对象的对象?
Rails - how to list each object's object in many-to-many relations?
我有多个具有连接表的模型,我想在控制器中实现这些项目的列表。
Customer <- many -> Products
Products <- many -> Teams
Products <- many -> Documents
型号:
class Customer < ActiveRecord::Base
has_many :cstprd_relations
has_many :products, :through => :cstprd_relations
end
class Product < ActiveRecord::Base
has_many :cstprd_relations
has_many :customers, :through => :cstprd_relations
has_many :teaprd_relations
has_many :teams, :through => :teaprd_relations
end
class Team < ActiveRecord::Base
has_many :teaprd_relations
has_many :products, :through => :teaprd_relations
end
class Document < ActiveRecord::Base
has_many :prddoc_relations
has_many :products, :through => :prddoc_relations
end
例如,文档迁移:
class CreateDocuments < ActiveRecord::Migration
def change
create_table :documents do |t|
t.string :name
t.timestamps null: false
end
end
end
class AddDocumentRefToProducts < ActiveRecord::Migration
def change
add_reference :products, :document, index: true, foreign_key: true
end
end
如何列出属于产品的唯一文档并将它们 link 给团队(文档)?
像这样:
客户 1:
- “产品 'Calc' 的文档 'Testing phase 1' 已由团队完成
'QA, TestTeam, Dev'"
- “产品 'Phone' 的文档 'Testing phase 2' 已由团队完成
'QA, TechSpec, MarketingTeam'"
- "Document 'Install Guide' for product 'App' is finished by teams 'Dev, TechSpec'"
不确定我是否理解这个问题,但您可以将 ActiveRecord 关联与 where 子句混合匹配,如下所示:
team_4_documents = Document.where(product_id: Team.find(4).products).all
( .find(4) 这里是一个让 Team 成为特定 Team 的例子)
也许这段代码展示了它的样子。
Product.all.each do | prod |
Document.products( prod.id ).teams.each do | team |
# generate message
end
end
或基于文档
Document.where( :id => id ).each do | doc |
doc.products.each do | prod |
prod.teams.each do | team |
# generate you message
end
end
end
编辑
在评论中回答问题
Customer.all.each do | cust |
puts "Customer #{cust.name}"
cust.products.each do | prod |
prod.documents.each do | doc |
print "* Document #{doc.name} for product #{prod.name} is finished by teams "
puts "'" + prod.teams.map(&:name).join(', ') + "'"
end
end
end
我有多个具有连接表的模型,我想在控制器中实现这些项目的列表。
Customer <- many -> Products
Products <- many -> Teams
Products <- many -> Documents
型号:
class Customer < ActiveRecord::Base
has_many :cstprd_relations
has_many :products, :through => :cstprd_relations
end
class Product < ActiveRecord::Base
has_many :cstprd_relations
has_many :customers, :through => :cstprd_relations
has_many :teaprd_relations
has_many :teams, :through => :teaprd_relations
end
class Team < ActiveRecord::Base
has_many :teaprd_relations
has_many :products, :through => :teaprd_relations
end
class Document < ActiveRecord::Base
has_many :prddoc_relations
has_many :products, :through => :prddoc_relations
end
例如,文档迁移:
class CreateDocuments < ActiveRecord::Migration
def change
create_table :documents do |t|
t.string :name
t.timestamps null: false
end
end
end
class AddDocumentRefToProducts < ActiveRecord::Migration
def change
add_reference :products, :document, index: true, foreign_key: true
end
end
如何列出属于产品的唯一文档并将它们 link 给团队(文档)?
像这样:
客户 1:
- “产品 'Calc' 的文档 'Testing phase 1' 已由团队完成 'QA, TestTeam, Dev'"
- “产品 'Phone' 的文档 'Testing phase 2' 已由团队完成 'QA, TechSpec, MarketingTeam'"
- "Document 'Install Guide' for product 'App' is finished by teams 'Dev, TechSpec'"
不确定我是否理解这个问题,但您可以将 ActiveRecord 关联与 where 子句混合匹配,如下所示:
team_4_documents = Document.where(product_id: Team.find(4).products).all
( .find(4) 这里是一个让 Team 成为特定 Team 的例子)
也许这段代码展示了它的样子。
Product.all.each do | prod |
Document.products( prod.id ).teams.each do | team |
# generate message
end
end
或基于文档
Document.where( :id => id ).each do | doc |
doc.products.each do | prod |
prod.teams.each do | team |
# generate you message
end
end
end
编辑 在评论中回答问题
Customer.all.each do | cust |
puts "Customer #{cust.name}"
cust.products.each do | prod |
prod.documents.each do | doc |
print "* Document #{doc.name} for product #{prod.name} is finished by teams "
puts "'" + prod.teams.map(&:name).join(', ') + "'"
end
end
end