Rails有一个关联未链接

时间:2019-07-10 17:59:35

标签: ruby-on-rails ruby

我当前正在尝试在用户和实体之间创建关联。

class Entity < ApplicationRecord
    has_one :first_level_approver, class_name: "User"
end

class User < ApplicationRecord
    belongs_to :entity, optional: false
end

在Rails控制台中,当我执行类似...

e = Entity.first
entity.first_level_approver = User.first

它将自动更新用户,使其具有与e中的实体相同的实体ID。它不会将实体分配给用户的第一级批准者。我的协会设置正确吗?如果可以,我该怎么做才能将first level approver用户类分配给一个实体。

这是我的模式

create_table "entities", force: :cascade do |t|
    t.string "name"
    t.string "uuid"
    t.boolean "root", default: false
    t.bigint "secretary_id"
    t.bigint "first_level_approver_id"
    t.bigint "second_level_approver_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["first_level_approver_id"], name: "index_entities_on_first_level_approver_id"
    t.index ["second_level_approver_id"], name: "index_entities_on_second_level_approver_id"
    t.index ["secretary_id"], name: "index_entities_on_secretary_id"
  end 

create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.string "first_name"
    t.string "last_name"
    t.string "role", default: "teacher"
    t.string "uuid"
    t.bigint "entity_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["entity_id"], name: "index_users_on_entity_id"
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

0 个答案:

没有答案