Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 在rails中创建一个表并添加外键约束大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有field WARD_id的表学生,我必须创建一个名为guardian_users的表,其中包含字段id,WARD_id,email,guardian_id,hashed_pa​​ssword等.

现在我必须添加约束外键.学生中的任何更新/删除/编辑/插入都应对guardian_users产生相同的影响.

我怎样才能在rails 2.3.5中做到这一点?

表学生存在但其他人尚不存在.

@H_675_9@解决方法
您将需要 foreign_key_migrations插件或#execute方法.假设你使用插件:
class CreateGuardianUsersTable < ActiveRecord::Migration
  def self.up
    create_table(:guardian_users) do |table|
      table.@R_801_6561@amps # I find them useful; ymMV
      table.Integer :guardian_id
      table.Integer :WARD_id,:null => false,:references => [:students,:id]
      table.String :email
      table.String :heahead_password
    end
  end

  def self.down
    drop_table :guardian_users
  end
end

你的模型中:

class GuardianUser < ActiveRecord::Base
  belongs_to :student
end

class student < ActiveRecord::Base
  has_many guardian_users:,:foreign_key => 'WARD_id',:class_name => 'GuardianUser',:dependent => :destroy
end

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 在rails中创建一个表并添加外键约束全部内容,希望文章能够帮你解决ruby-on-rails – 在rails中创建一个表并添加外键约束所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。