Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of model associations... #20

Open
rhnorment opened this issue Dec 10, 2014 · 2 comments
Open

Example of model associations... #20

rhnorment opened this issue Dec 10, 2014 · 2 comments
Labels

Comments

@rhnorment
Copy link

Hi, I really appreciate your work and I'm confident I can get it working with a little help.

My app has 3 member classes: users, storybooks, and stories. I have the following line in my Group model:

groupify :group, members: [:users, :storybooks, :stories], default_members: :users

Am I to assume that the Groupify gem DOES NOT take care of certain parameters within an association? For example, I am able to add users to groups, but when I delete a group, I want to also delete the GroupMembership records associated with that group, such as with dependent: :destroy. How can I accomplish this?

Do I also need to specify that a user has_many group_memberships, etc, etc, etc or are these inherent in the Groupify gem?

Thanks in advance.

@dwbutler
Copy link
Owner

Hi, thanks for using Groupify!

I think you'll find that Groupify generally does the "right thing" when setting up ActiveRecord associations.

For example, when a group is destroyed, the associated group memberships are also destroyed, as you expected. You can see this being set up in the ActiveRecord Group module:

has_many :group_memberships, :dependent => :destroy

You'll also find that group members get the correct associations set up as well via the GroupMember module:

# Example of what gets run under Rails 4
has_many :group_memberships, as: :member, autosave: true, dependent: :destroy
has_many :groups, ->{ uniq }, through: :group_memberships, class_name: @group_class_name, extend: GroupAssociationExtensions

Groupify really does all its magic through association functionality built into ActiveRecord. You probably shouldn't need to customize the parameters sent to these associations. But if you have a use case I haven't thought of, please let me know!

I do think that a section in the readme describing how Groupify works internally would be helpful.

@dwbutler
Copy link
Owner

You'll also find that the behavior is pretty well spec'ed. For example, here's the spec showing that group memberships get destroyed when a group is destroyed.

That might give you some confidence that Groupify is doing the correct thing, if you don't feel like exploring through the source code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants