簡體   English   中英

設計用戶注冊后創建實例,rails

[英]create an instance after devise user registration, rails

在用戶通過設計注冊后,我想自動創建不相關的模型實例,只是想知道我將在哪里以及如何去做?

就像是

@fan.subscriptions.create(band_id: 1)

成功注冊后(用戶為粉絲)

訂閱模式

create_table "subscriptions", id: :serial, force: :cascade do |t|
    t.integer "fan_id"
    t.boolean "special", default: false
    t.integer "band_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "confirmed"
    t.index ["band_id"], name: "index_subscriptions_on_band_id"
    t.index ["fan_id"], name: "index_subscriptions_on_fan_id"
  end

您可以在fan模型中使用回調,例如:

after_create :subscribe

如果您有一對多的關系:

def subscribe
  subscriptions.where(band_id: band_id).first_or_create
end

希望能幫助到你..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM