-
Notifications
You must be signed in to change notification settings - Fork 43
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
failed to add a named group #65
Comments
Can I see your full Groupify configuration in your models? I suspect that something isn't set up correctly. |
Here's my configuration: class Organization < ApplicationRecord
groupify :group
end app/models/organization_membership.rb class OrganizationMembership < ApplicationRecord
groupify :group_membership
end app/models/user.rb class User < ApplicationRecord
groupify :group_member
groupify :named_group_member
end config/initializers/groupify.rb Groupify.configure do |config|
config.group_class_name = 'Organization'
config.group_membership_class_name = 'OrganizationMembership'
end The migration class GroupifyMigration < ActiveRecord::Migration[5.1]
def change
change_table :organizations do |t|
t.string :type
end
create_table :organization_memberships do |t|
t.references :member, polymorphic: true, index: true, null: false
t.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 with
t.string :membership_type
t.timestamps
end
end
end and here is the full error (17.8ms) SELECT `organization_memberships`.`group_name` FROM `organization_memberships` WHERE `organization_memberships`.`member_id` = 2 AND `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` = 2 AND `organization_memberships`.`member_type` = 'User' AND `organization_memberships`.`group_name` = 'admin' AND `organization_memberships`.`membership_type` IS NULL ORDER BY `organization_memberships`.`id` ASC LIMIT 1
User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
(0.3ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Group must exist |
You've defined |
This means i can't use them both, groups and named groups ? |
I'm having the same issue — here's a sample repo https://github.com/reentim/groupify_example, which only calls |
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, In Rails 5, you can add |
I verified that adding |
I was able to fix it in both Rails 4 and 5 by adding |
Rails 5 made `belongs_to` associations required by default. When using named groups, the group association is expected to be empty. This fixes the issue in a Rails 4 compatible way by adding `required: false` Fixes #65
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 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. |
I confirm what @gaetan-pc said switching to the |
After doing
user = User.first
user.named_groups << :admin
I got
(0.3ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Group must exist
Do I have to create the group first ? how?
The text was updated successfully, but these errors were encountered: