导轨 - 父/子关系 [英] Rails - Parent/child relationships

查看:81
本文介绍了导轨 - 父/子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用一个标准的一对一关系来处理父/子关系:

I'm currently using a standard one-to-one relationship to handle parent/child relationships:

class Category < ActiveRecord::Base
  has_one :category
  belongs_to :category
end

有没有推荐的方法做,或者这是好吗?

Is there a recommended way to do it or is this ok?

推荐答案

您需要调整您使用的是得到这个工作名称 - 您指定的关系的名称,然后告诉AR类是什么:

You will need to tweak the names you are using to get this working - you specify the name of the relationship, and then tell AR what the class is:

class Category < ActiveRecord::Base
  has_one :child, :class_name => "Category"
  belongs_to :parent, :class_name => "Category" 
end

这篇关于导轨 - 父/子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆