开发者

Rails / Devise: Create user and parent org at once

Using Devise.

Models:

User belongs_to Organization
Orga开发者_JS百科nization has_many Users

During signup, I want to create the user's parent organization as well. So two pieces to the form: 1) organization info, and 2) basic user info (email/password)

I've done a bunch of searching for Devise and nested resources, but they usually talk about the model relationship being the other direction (User has_many).

Any ideas?

Thanks in advance!


Do you already know the organisations that the user could belong to?

In which case just have a drop down when they register and insert the ID of the organisation on save.

Otherwise, what will probably happen is that you basically end up with a 1:1 with organisation anyway given typos and so on, unless you are guessing based on the name they input. Does the organisation have any kind of security associated with it? If this is a public site it seems a little dangerous because people could camp in places they're not supposed to be.

That said:

o = Organisation.find_or_create_by_name(params[:org_name])
u = o.user.build(params[:user])
if u.save ... # etc.

or something like that.


u = User.new(params[:user])

if u.valid?
  o = Organisation.create(params[:org_name])
  u.organization = o
  u.save
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜