如何将一个belongs_to添加到rails中的同一个表中

时间:2016-01-02 05:49:25

标签: ruby-on-rails

嗨,我在Rails中有学生表

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students do |t|
      t.belongs_to :school, index: true, foreign_key: true,null: false
      t.belongs_to :user, index: true, foreign_key: true,null: false
      t.belongs_to :division, index: true, foreign_key: true,null: false

      t.belongs_to :section, index: true, foreign_key: true,null: false

      t.timestamps null: false
    end
  end
end

,现在我想在该表中添加两个foriegn键

第一个父亲,其中父亲属于用户表,而在DB中,其角色保存是父亲

第二位母亲,其中母亲属于用户桌,而在DB中,其角色保存为母亲

我是rails的新手,试试这个

t.belongs_to :user, index: true,foreign_key:  'father_id',null: false

但它没有在学生表中创建father_id

我想像student.father那样访问父亲。请帮我这样做

1 个答案:

答案 0 :(得分:2)

您正在查看的是self referential join

Dim s As Shape

Dim p As Page, numberPage As Integer
Dim i&

Dim WhatSamp As String

WhatSamp = "Sample1"

For i = 1 To ActiveDocument.Pages.Count
ActiveDocument.Pages(i).Activate

For Each s In ActiveDocument.ActivePage.Shapes

If s.Type = cdrTextShape Then

     If InStr(1, s.Text.Story, WhatSamp) > 0 Then
     ActivePage.Layers("Sample").Visible = True
     ActivePage.Layers("Sample").Printable = True

     End If

End If

Next

Next i