You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classGroupifyMigration < ActiveRecord::Migration[5.1]defchangechange_table:organizationsdo |t|
t.string:typeendcreate_table:organization_membershipsdo |t|
t.references:member,polymorphic: true,index: true,null: falset.references:group,polymorphic: true,index: {name: "index_org_membership_on_org_type_and_org_id"}# The named group to which a member belongs (if using)t.string:group_name,index: true# The membership type the member belongs witht.string:membership_typet.timestampsendendend
and here is the full error
(17.8ms) SELECT`organization_memberships`.`group_name`FROM`organization_memberships`WHERE`organization_memberships`.`member_id`=2AND`organization_memberships`.`member_type`='User'AND (group_name IS NOT NULL)
(0.4ms) BEGIN
OrganizationMembership Load (0.7ms) SELECT`organization_memberships`.*FROM`organization_memberships`WHERE`organization_memberships`.`member_id`=2AND`organization_memberships`.`member_type`='User'AND`organization_memberships`.`group_name`='admin'AND`organization_memberships`.`membership_type` IS NULLORDER BY`organization_memberships`.`id`ASCLIMIT1
User Load (0.6ms) SELECT`users`.*FROM`users`WHERE`users`.`id`=2LIMIT1
(0.3ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Group must exist
You've defined User to be a group member by calling groupify :group_member, but there is no Group model defined. Try removing that line if you're planning to only used named groups and not groups.
I'm having the same issue — here's a sample repo https://github.com/reentim/groupify_example, which only calls groupify :named_group_member, but still runs into ActiveRecord::RecordInvalid: Validation failed: Group must exist when adding a named group.
@reentim@kainio Do you get this error with the updates from #61? I don't, but let me know to determine if it's a configuration or groupify internals issue. Thanks.
In Rails 5, belongs_to associations are now required by default. Groupify's group membership model defines belongs_to :group, which means that group is now required whether you want it to be or not.
In Rails 5, you can add optional: true to the association to make it optional. But I'm not sure if doing this will break Rails 4 support. Will do some more testing.
The Travis build results are in, and this fix doesn't work in Rails 4.0 or 4.1 since they don't support the required configuration key.
The best approach will probably be to have split configuration based on the Rails version as a short term fix. And then in the next major release of Groupify we'll drop support for Rails < 4.2.
Thanks @dwbutler ! Same as @reentim and @kainio I had the error message ActiveRecord::RecordInvalid: Validation failed: Group must exist when trying to add User to a named group (default groupify configuration on Rails 5.1). Switching to the fix/rails5-named-group branch fix the issue for me.
@kainio @gaetan-pc @reentim#61 drops support for Rails < 4.2 and this fix has been merged. If you could, please let us know how the refactor fares in your applications. Thanks!
Activity
dwbutler commentedon Jul 12, 2017
Can I see your full Groupify configuration in your models? I suspect that something isn't set up correctly.
kainio commentedon Jul 13, 2017
Here's my configuration:
app/models/organization.rb
app/models/organization_membership.rb
app/models/user.rb
config/initializers/groupify.rb
The migration
and here is the full error
dwbutler commentedon Jul 17, 2017
You've defined
Userto be a group member by callinggroupify :group_member, but there is no Group model defined. Try removing that line if you're planning to only used named groups and not groups.kainio commentedon Aug 1, 2017
This means i can't use them both, groups and named groups ?
Because I am using also groups as organisations.
reentim commentedon Aug 2, 2017
I'm having the same issue — here's a sample repo https://github.com/reentim/groupify_example, which only calls
groupify :named_group_member, but still runs intoActiveRecord::RecordInvalid: Validation failed: Group must existwhen adding a named group.joelvh commentedon Aug 4, 2017
@reentim @kainio Do you get this error with the updates from #61? I don't, but let me know to determine if it's a configuration or groupify internals issue. Thanks.
dwbutler commentedon Aug 4, 2017
After some investigation, I believe the issue is caused by this change in Rails 5: http://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html
In Rails 5,
belongs_toassociations are now required by default. Groupify's group membership model definesbelongs_to :group, which means that group is now required whether you want it to be or not.In Rails 5, you can add
optional: trueto the association to make it optional. But I'm not sure if doing this will break Rails 4 support. Will do some more testing.dwbutler commentedon Aug 4, 2017
I verified that adding
optional: trueto the association fixes the issue in Rails 5. But under Rails 4, I getUnknown key: :optional👎 😢dwbutler commentedon Aug 4, 2017
I was able to fix it in both Rails 4 and 5 by adding
required: false🤕Fix named group support in Rails 5
dwbutler commentedon Aug 4, 2017
@reentim @kainio See if the above commit fixes the issue.
dwbutler commentedon Aug 4, 2017
The Travis build results are in, and this fix doesn't work in Rails 4.0 or 4.1 since they don't support the
requiredconfiguration key.The best approach will probably be to have split configuration based on the Rails version as a short term fix. And then in the next major release of Groupify we'll drop support for Rails < 4.2.
tanega commentedon Aug 4, 2017
Thanks @dwbutler ! Same as @reentim and @kainio I had the error message
ActiveRecord::RecordInvalid: Validation failed: Group must existwhen trying to addUserto a named group (default groupify configuration on Rails 5.1). Switching to thefix/rails5-named-groupbranch fix the issue for me.kainio commentedon Aug 5, 2017
I confirm what @gaetan-pc said switching to the
fix/rails5-named-groupsfixed the problem.Maybe it should be merge to master.
joelvh commentedon Sep 7, 2017
@kainio @gaetan-pc @reentim #61 drops support for Rails < 4.2 and this fix has been merged. If you could, please let us know how the refactor fares in your applications. Thanks!