Rails / Devise: Create user and parent org at once - ruby-on-rails

Using Devise.
Models:
User belongs_to Organization
Organization 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

Related

Associate a Readable User Dropdown in ActiveAdmin

I guess not much help here. When you are trying to associate a user from a dropdown list, and imagine if you have 1000000 users, you would see the user instance. I need it to show the actual user's email address.
app/models/user.rb:
has_one :company
app/models/company.rb:
belongs_to :user
The link I was given has nothing to do with the dropdown's value.
Have I setup my rails association incorrectly? Funny thing was, using rails admin I had no issue in this department as I could associate a company when creating a user but not so with active admin.
All I want is when I select the User dropdown, as in picture, I'd see a list of user email addresses.
Tim was correct all this time. I needed to create a function in the User model.
app/models/user.rb:
def display_name
email
end

Rails find using where in one to many relation

I have a User model that has many "states":
class User
has_many states
class State
belongs_to user
Now, I'd like to go through each User and find all the Users with that state something like...
state = State.last
# I want to get all the users where state appears in ANY user's user.states
User.where(state IN user.states.. )
Any advice would be appreciated!
One way is to use a join to get the user's that have the state:
User.joins(:states).where(state: 'some_type_of_state')
The other way is by grabbing all the user id's that have the specific state, and then querying for those users:
user_ids = State.where(state: 'some_type_of_state').pluck(:user_id)
users = User.where(id: user_ids)
The last query typically doesn't perform as well.

Is there any way to implement a "has one of each" relationship in rails?

Say I have three models with users, roles, and events. Each user can have many roles (e.g. guitarist, bassist, etc) and each role can have one or more users (e.g. Ted can act as guitarist or a bassist) who can fulfil it.
I'm creating a rota application for a church band, so it means that every event has ONE OF EACH role in the band and one user to fill each role per event. I was wondering there was any way of enforcing this relationship in the model or whether I had to do this somewhere else in the application. Thanks for any help in advance!
Event should have many users through EventParticipation and EventParticipation should
belongs_to :user
belongs_to :event
Put some uniqueness validation for user_id, add a role_id and do a scoped uniqueness validation if you want.
I would do this with custom validation methods in the model. Here's some untested code but something like
class ChurchEvent < ActiveRecord::Base
validate :has_one_role_each
def has_one_role_each
errors.add(:base, "only one of each role allowed") if duplicate_roles?
end
private
def duplicate_roles?
# assuming band_roles is array of roles for this event
band_roles.uniq.count != band_roles.count
end
end
And something similar for the users.

Figuring out relationships for a Document model that can be editted by multiple users

I'm trying to figure out how to set up the relationship/association for a Document model for my rails project. I have a User model and Users can friend each another (friendship model).
Now I want users to be able to invite (give access to) CERTAIN friends to modify and edit these documents similar to Google Docs.
This is my current approach to this problem:
Create a new relationship model called "group", essentially a subset of friends. Document belongs to a user and document belongs to a group. Users can then invite their friends into these group relationships and documents can be accessed/modified through these groups.
Therefore, User has many groups and group belongs to many users.
My question:
Is there a better approach to this problem?
You might consider a "Membership" join table. So:
Group >- Membership -< User
...since you requirement is some users will have abilities others wont (editing). There are all sorts of options from there, for example STI which would give you:
class Membership < ActiveRecord::Base
...general membership functions
end
class Editor < Membership
...editor specific code...
end
class Reviewer < Membership
...reviewer specific code...
end
And a controller class something like
class MembershipController
def create invitation
if invitation.for_editor?
# assuming invitation has one group, and group_memberships method that returns group.memberships
invitation.group_memberships.create! user: current_user
else
invitation.group_memberships.create! user: current_user
end
end
end
As an example, depending on your opinion of STI.
https://github.com/scambra/devise_invitable
might have more ideas.

Creating an additional related model with Devise

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.
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?
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.

Resources