在模型关系中指定其他外键

时间:2018-07-05 06:05:53

标签: ruby-on-rails

我有两个模型,即Staff和Hospital,它们之间的归属关系为has_many。医院有很多工作人员。我的职员表如下:

class CreateStaffs < ActiveRecord::Migration[5.1]
  def change
    create_table :staffs do |t|
      t.string :Title
      t.string :FirstName
      .
      .
      .
      t.integer :hospital_id

      t.timestamps
    end
  end
end

我不能指定其他外键,例如hospital_name吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您需要创建这样的表:

private final Object lock = new Object();

public void writeToNext(String dirPath) {
    synchronized(lock) {
        File dir = new File(dirPath);
        List<File> files = Arrays.asList(dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return !pathname.isDirectory();
            }
        }));

        int numFiles = files.size();
        String nextFile = dir.getAbsolutePath() + File.separator + (numFiles + 1) + ".txt"; // get a path for the new file
        System.out.println("Writing to " + nextFile);
        // TODO write to file
    }
}

create_table :staffs do |t| ... t.string :hospital_name end add_foreign_key :staffs, :hospitals, column: :hospital_name, primary_key: :name 中引用primary_key: :name表的列名。