繁体   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