User and Organization have a many-to-many association through Relationship.
The Relationship model, amongst other things, specifies whether the User is a member of the Organization (member boolean).
If a new Organization together with a new User related to that Organization needs to be created I currently do the following:
Organization.create!(name: "name", ...).users.create(email: "email@email.com",...)
However, this does not yet set the member boolean in the Relationship model to true.
Is there a way to include this in the above command? Or is this only possible separately from creating the user and organization record? Because then it would require relatively a lot of code and seems a bit inefficient to add after the previous create line:
@user = User.find_by(username: username)
@organization = Organization.find_by(name: name)
@relationship = @organization.relationships.find(@user)
@relationship.update_attributes(member: true)
Aucun commentaire:
Enregistrer un commentaire