2017-05-08 40 views
2

在Rails 5中,我想創建一個Author模型的新實例,但是在這個過程中我只想從我的User模型中複製屬性值(列)的一個子集。Rails如何從ActiveRecord對象中提取(並複製)某些屬性?

我試圖運行下面的代碼:

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:未定義的方法`each'for nil:NilClass error。

下面是從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 

正如你可以看到,新的作者表中有很多的與User表相同的列/屬性,但我只想在創建新Author對象時複製用戶屬性(列和值)的子集。

也許user.attributes.without方法不是正確的嗎?

回答

1

您可以通過slice來實現,因爲user.attributes只是一個值的散列。

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

這很有效,謝謝! –

相關問題