本文介绍了Rails重命名协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,TreeNode和User.每个用户has_one TreeNode,这是树的根.

I have two models, TreeNode and User. Each user has_one TreeNode, which is the root of the tree.

class TreeNode
  acts_as_tree
  belongs_to :user
end

class User
  has_one :tree_node
end

我想进行此设置,以便Rails进行关联,以便我可以做类似的事情

I would like to have this setup so that rails will make the association so that I can do something like

User.first.tree

代替

User.first.tree_node

人们将如何做这样的事情?

How would one go about doing something like this?

推荐答案

has_one :tree, :class_name => "TreeNode"

请记住,这里假定tree_nodes表中的外键名为user_id.

Keep in mind, this assumes a foreign key called user_id in the tree_nodes table.

编辑:如果这样不起作用,您可能可能需要指定外键(:foreign_key => :user_id),但我认为不是.

Edit: If that doesn't work, you might need to specify the foreign key (:foreign_key => :user_id), but I don't think so.

这篇关于Rails重命名协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:38