Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Users and groups: belongs_to relationship

edited June 2014 in Framework

I have an app called groups and I'd like to establish a relationship where users belong to a group. I'm overriding the elefant user app for some of the functionality, and I know how to set that up, but how can I extend the user model to add the relationship? I created a model file, but I don't think elefant knows to look for it.

Sorry if the question is unclear.


  • To create a one-to-many relationship of users to groups, you would need to add a new field to the user table/model.

    I've been planning on adding some new features around users (recently added a bunch more built-in contact fields already), and was thinking a better solution might be groups being "Organizations" with their own contact info as well, and allowing users to be part of multiple ones. The many-many relationship would then need a join table instead of a new field in the user table.

    Let me know if that's similar to what you're thinking.

  • My situation is that many users can belong to the same group. I added group_id to the user table, and I can query it with custom sql, but I was wanting to use the elefant relationship tools.

    I think built-in organizations/groups would be slick. I'm not sure how common a need it is, but it's certainly relevant for my current project.

  • I still have some planning to do, but will probably add it so you can have many users in each org/group, and a user can belong to multiple orgs as well.

    The relationships between models are documented here:

    While you can specify sorting of the returned related objects, it's often still a bit limited and I've found that often a custom SQL query is still needed.

  • Yeah, I've used and appreciated elefant's relationships before, but not with the user table. I don't want to add it to apps/user/models/User.php, but I'm not sure where to add it.

  • To add the relationship, you do have to modify the model classes themselves.

  • Right, but I'm leery about modifying apps/user/models/User.php and I don't know how to tell elefant to find my model in apps/myusercustomizations/models/MyUser.php

    Probably it's obvious, or else I'm just not making my question clear.

  • The user app relies on the User class, but it should be safe to modify if you're just adding a relationship to another model, and especially if you're versioning your changes too :) Then you should be pretty safe to do so.

  • I guess it's time to start versioning... or just stick with SQL. (Not sure why I'm resistant. I guess I found git confusing when I investigated and haven't taken the time to figure out how to make it do what I want. I feel like it's bossing me around instead of vice-versa.)

    In another thread you mentioned BitBucket. What's the advantage of keeping repositories online as opposed to on my workstation? What I've been doing is keeping a mirror of your master on my hard drive and then integrating the changes to my local sites via a bash script using rsync.

  • The benefit of git and remote repositories is mainly in collaborating with others, but it also serves as a remote backup copy (just git push origin master and you're backed up!). It's also easy to just do a git pull elefant master from time to time to keep things in sync.

    Bitbucket's benefit over Github is mainly just the free private repository hosting, so you can keep some things private. But it works the exact same way with Git itself.

Sign In or Register to comment.