dwbutler / groupify

Add group and membership functionality to your Rails models
MIT License
194 stars 42 forks source link

Example of model associations... #20

Open rhnorment opened 9 years ago

rhnorment commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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!