在我的应用程序中,当新用户创建或签名时,他的帐户中应该有一个名为“用户”的默认项目。如何制作?
我正在使用设计用于登录和注册用户。用户登录后,他可以创建一个项目并进行处理。但是现在我想要在用户注册时出现默认项目。
答案 0 :(得分:0)
所谓的callbacks会很好地为您服务:
在您的模型中,您添加类似于:
的内容<强> user.rb 强>
# this calls the function defined below after the creation of a user
after_create :default_project
# put callback functions into the private sector, as it will only be used for this model
private
def default_project
Project.create(:user_id => this.id, :name => "Users", ...)
# do more stuff
end