相关疑难解决方法(0)

如何在Rails中管理没有id的表?

我有两个模型:人和关系.第二个存储有关两个人之间关系的信息.它有parent_id和child_id字段,并且没有id字段.我用has_many连接它:通过它可以工作.

但:

  1. 即使表中存在某些关系,Relation.find(:all)也会返回空数组(因为没有id字段).
  2. 我不知道如何删除关系.

我的模型看起来像这样:

class Person < ActiveRecord::Base
  has_many :child_relations, 
           :class_name => "Relation", 
           :foreign_key => "parent_id"
  has_many :parent_relations, 
           :class_name => "Relation", 
           :foreign_key => "child_id"

  has_many :children, :through => :child_relations
  has_many :parents, :through => :parent_relations
end

class Relation < ActiveRecord::Base
  belongs_to :parent, :class_name => "Person"
  belongs_to :child, :class_name => "Person"
end
Run Code Online (Sandbox Code Playgroud)

有什么建议?

更新:我使用过has_many:通过 becouse我还存储有关表中关系类型的信息.目前我放弃了,我在我的表中添加了id字段(Rails约定......).但我的问题仍未解决.

ruby-on-rails

3
推荐指数
1
解决办法
5994
查看次数

标签 统计

ruby-on-rails ×1