gpt4 book ai didi

ruby-on-rails - Rails 如何仅从 ActiveRecord 对象中提取(和复制)某些属性?

转载 作者:行者123 更新时间:2023-12-04 05:45:02 25 4
gpt4 key购买 nike

在 Rails 5 中,我想创建一个 Author 模型的新实例,但在此过程中,我只想从我的 User 模型中复制属性值(列)的一个子集。

我正在尝试运行以下代码:

user = User.first
Author.create user.attributes.without("id", "token", "secret", "created_at", "updated_at", "active", "email", "encrypted_password", "first_name", "last_name", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip")

User.first 方法有效并从表中返回一个有效用户。但是, Author.create 行不起作用。我收到一个 NoMethodError: undefined method `each' for nil:NilClass 错误。

这是我来自 schema.rb 文件的用户表:
create_table "users", force: :cascade do |t|
t.string "provider", null: false
t.string "uid", null: false
t.string "name"
t.string "location"
t.string "image_url"
t.string "url"
t.string "token"
t.string "secret"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "active", default: false
t.text "description"
t.string "screen_name"
t.integer "followers"
t.integer "following"
t.integer "statuses"
t.integer "listed"
t.integer "lists"
t.integer "favorites"
t.integer "views", default: 0
t.string "website"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "first_name", default: "", null: false
t.string "last_name", 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.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.index ["active"], name: "index_users_on_active", using: :btree
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["provider", "uid"], name: "index_users_on_provider_and_uid", unique: true, using: :btree
t.index ["provider"], name: "index_users_on_provider", using: :btree
t.index ["uid"], name: "index_users_on_uid", using: :btree
end

这是我的作者表:
create_table "authors", force: :cascade do |t|
t.string "uid", null: false
t.string "provider", null: false
t.string "name"
t.string "location"
t.string "image_url"
t.string "url"
t.text "description"
t.string "screen_name"
t.integer "followers", default: 0
t.integer "following", default: 0
t.integer "statuses", default: 0
t.integer "listed", default: 0
t.integer "lists", default: 0
t.integer "favorites", default: 0
t.integer "views", default: 0
t.string "website"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["provider"], name: "index_authors_on_provider", using: :btree
t.index ["uid", "provider"], name: "index_authors_on_uid_and_provider", unique: true, using: :btree
t.index ["uid"], name: "index_authors_on_uid", using: :btree
end

如您所见,新的 Author 表具有许多与 User 表相同的列/属性,但我只想在创建新的 Author 对象时复制 User 属性(列和值)的子集。

也许 user.attributes.without 方法不是正确的方法?

最佳答案

您可以通过 slice 来完成自 user.attributes只是值的散列。

user.attributes.slice('name', 'location')

关于ruby-on-rails - Rails 如何仅从 ActiveRecord 对象中提取(和复制)某些属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43838675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com