用Devise创建一个附加的相关模型 [英] Creating an additional related model with Devise
问题描述
我已经开始使用Devise实现一个新项目,这对处理用户来说非常棒。然而,当用户注册时,他们不只是创建一个用户
模型,而且还需要创建一个相关的帐户
代表公司的模型。额外的用户还将 belongs_to
这个帐户
模型。
I've started to implement a new project using Devise, which is pretty fantastic for handling users. However, when a user signs up, they're not just creating a User
model, but also need to create a related Account
model that represents the company. Additional users will also belongs_to
this Account
model.
在Devise中似乎找不到一个钩子,虽然它似乎是一个很常见的模式。这个最好的做法是什么?
I can't seem to find a hook for this in Devise, though it seems like a pretty common pattern. What's the best practice for this?
我还应该提到,帐户
有几个字段需要在sign_up表单上提供,所以在 User
模型中只是这样:
I should also mention that there are a couple of fields for the Account
that need to be provided on the sign_up form, so just something like this in the User
model:
after_create :make_sure_account_exists
def make_sure_account_exists
if self.account.nil?
@account = self.create_account({ :company_name => '???' })
end
..因为我不知道如何获得公司名称。
.. as I'm not sure how to get the company name.
推荐答案
你可以考虑在用户注册表单中添加帐户字段,如下所示:
Devise用户配置文件模型?
You could consider adding Account fields in the User registration form, like here: Profile model for Devise users?
这篇关于用Devise创建一个附加的相关模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!