Comments (14)
I was able to fix it in both Rails 4 and 5 by adding required: false
π€
from groupify.
Can I see your full Groupify configuration in your models? I suspect that something isn't set up correctly.
from groupify.
Here's my configuration:
app/models/organization.rb
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
from groupify.
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.
from groupify.
This means i can't use them both, groups and named groups ?
Because I am using also groups as organisations.
from groupify.
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.
from groupify.
@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.
from groupify.
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_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.
from groupify.
I verified that adding optional: true
to the association fixes the issue in Rails 5. But under Rails 4, I get Unknown key: :optional
π π’
from groupify.
@reentim @kainio See if the above commit fixes the issue.
from groupify.
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.
from groupify.
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.
from groupify.
I confirm what @gaetan-pc said switching to the fix/rails5-named-groups
fixed the problem.
Maybe it should be merge to master.
from groupify.
@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!
from groupify.
Related Issues (20)
- Cannot get single table inheritance work HOT 4
- nesting groups inside groups HOT 7
- Adding users to groups HOT 3
- Cannot properly use groupify when using prefixed table names HOT 1
- Removing Users From a Group [QUESTION] HOT 2
- `in_only_groups` doesn't match on group IDs HOT 4
- changed group_membership but it still looking for a table group_membership HOT 3
- Question re Group Names HOT 1
- Named Groups? (similar to GitHub organizations data model) HOT 2
- Relationship between two models HOT 2
- Rails 5.1.5 Support HOT 2
- Two Quick Questions David! (RE: Levels, and Multiple Groups) HOT 2
- Two More Quick Questions! (RE: Creating Groups, & Classes) HOT 2
- Cannot retrieve group members PG
- Cannot retrieve group members in pg HOT 5
- ActiveRecord::RecordInvalid: Validation failed: Group must exist HOT 1
- Use exists? instead of Array#include? method
- How to make subgroups with groupify
- Rails 7 Updates? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from groupify.