如何从一对多关系切换为一对一关系

时间:2018-05-26 09:06:13

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord rails-migrations

在我的rails应用程序中,我有一个拥有许多公司且属于公司的用户。我想有效地迁移到用户只有一个公司但仍然属于公司的情况。是否足以将User.rb中的代码行更改为has_one :company

我已经确定在我的数据库中,所有用户只有一家公司。我想知道完成迁移的最有效方法。这是我对Company.rb belongs_to :user的示例代码。 对于公司创建行动,我有

  def create
    @company = Company.new(company_params)
    @company.user = current_user
    preview_company
  end

这是我的数据库架构

  create_table "companies", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.string "website"
    t.string "location"
    t.string "address"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.string "photo"
    t.string "logo"
    t.index ["user_id"], name: "index_companies_on_user_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.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "admin", default: false, null: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

0 个答案:

没有答案
相关问题