有谁知道方式,或者我知道如何做到这一点的地方?
基本上,我想要做的就是在两个表之间连接一个外键。
是真的,我所要做的就是写“belongs_to”和“have many”?
答案 0 :(得分:2)
您还需要确保与Class相关联的数据库表中的外键存在列,该列表示另一个“belongs_to”。所以对于班级......
Class Tree
belongs_to :forest
end
Class Forest
has_many :trees
end
... Rails假定您的trees
表格中有forest_id
列。然后,您可以执行
my_tree = Tree.find(1)
my_trees_forest = my_tree.forest
这是获取所需信息的好地方:http://guides.rubyonrails.org/association_basics.html