I’m working on a project that has users and groups, users can be members
of
a group or they can just be a user - what’s going to be the best way
to
deal with this?
I understand the normal has_many relationship just not sure how to do
this
one. Will try to post some code snippets tomorrow.
On Mon, Jun 24, 2013 at 8:54 AM, Cameron G. [email protected]
wrote:
users can be members of a group or they can just be a
user - what’s going to be the best way to deal with this?
has_many :through seems perfect for this, given the many-to-many
nature and the possibility you’ll want to have additional info about
the relationship (like when they joined, positions held within the
group, etc.).
-Dave
–
Dave A., the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/.
I’m working on a project that has users and groups, users can be members of
a group or they can just be a user - what’s going to be the best way to deal
with this?
If the user can only be a member of one group then use User belongs_to
group. If a particular member does not belong to a group then just
leave group_id nil. You will obviously have to allow for user.group
== nil in the code where appropriate. If he can belong to many (or
no) groups then use has_many :through as Dave suggested. You can test
user.groups to see if he is in any groups.
If the user can only be a member of one group then use User belongs_to
group.
Oh, good point. I was assuming (and you know what happens then!) that
a User could belong to many Groups.
-Dave
–
Dave A., the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/.
On Tue, Jun 25, 2013 at 9:55 PM, Cameron G. [email protected]
wrote:
:unprocessable_entity }
end
This will do (or at least try) the save twice, if the user has a
group. I’d recommend:
if it saves
redirect to (if it has a group, the group, else /)
else
give error msg
end
Also, look closely at the way you’re checking for a group. The first
option will trigger if the group is nil, the second if it’s not “”
(empty string)… but what if it is “”? That’s neither nil nor
not-“”, so it will take the “else” path and pretend like it didn’t
save. I suggest you use .blank? (or its inverse, .present?) to tell
if it’s there or not. Both nil and “” count as blank (and not
present).
-Dave
–
Dave A., the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/.
Here is an update, I have it working as only a group or as only a non-group
user. I have below the create controller and was wondering what would be the
best way to allow associations and non-associated records. Thanks
Here is an update, I have it working as only a group or as only a
non-group
user. I have below the create controller and was wondering what would be
the best way to allow associations and non-associated records. Thanks