【发布时间】:2021-02-19 06:28:40
【问题描述】:
我们要实现以下目标:
- 能够将“项目”与其他(多个)项目进行比较。
- 将比较参考保存在数据库中。
过去我们通过在数据库中存储一个数组来做到这一点,如下所示:
t.string "comparisons", default: [], array: true
现在我们正在考虑使用另一个表 - 除非有更好的方法?
最好是这样的:
我们已经有了这张桌子:
| projects |
|-----------|
| id | name |
| 1 | abc |
| 2 | def |
| 3 | ggg |
| 4 | fff |
我们要创建另一个类似的表:
| project_comparisons |
|-------------------------------|
| id | project_id | compared_id |
| 1 | 1 | 2 |
| 2 | 1 | 4 |
| 3 | 2 | 3 |
| 4 | 2 | 4 |
我们到底可以在哪里做这样的事情:
Project.find(1).project_comparisons.each do |x|
x.name
end
# Output:
'def'
'fff'
但我们在关系设置中迷失了方向。
这是我们目前所拥有的:
rails g model ProjectComparison project:references compared:references
# Migration file (edited)
create_table :project_comparisons do |t|
t.references :project, foreign_key: true
t.references :compared
end
class Project
has_many :project_comparisons
has_many :projects, through: :project_comparisons
# ... Here I think we need something in the line above?
end
class ProjectComparison
belongs_to :project
end
这现在有不正确的输出。 如果我们现在迭代这个例子:
Project.find(1).project_comparisons.each do |x|
x.name
end
# Output:
# aaa
# aaa
应该是'def'和'fff'
我能否以某种方式指定我们希望“compared_id”获取正确的项目,还是我们在这里走错了路?
【问题讨论】:
标签: ruby ruby-on-rails-3 activerecord