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

ActiveRecord::RecordInvalid: Validation failed: Group must exist #75

Open
denniswoot opened this issue Dec 13, 2018 · 1 comment
Open

Comments

@denniswoot
Copy link

denniswoot commented Dec 13, 2018

Hi, I am using the groupify gem. And encountered some errors when I would like to use named_groups.
I have tried the following solution. I am using rails 5.1

#57

a = User.find 1
=> #<User id: 1, email: "dennis@ibm.com", created_at: "2018-12-13 10:25:57", updated_at: "201
8-12-13 10:25:57">

a.groups

ActiveRecord::RecordInvalid: Validation failed: Group must exist

class GroupifyMigration < ActiveRecord::Migration[5.1]
  def change
    create_table :groups do |t|
      t.string     :name
    end

    create_table :group_memberships do |t|
      t.references :member, polymorphic: true, index: true, null: false
      t.references :group, polymorphic: true, index: true

      # 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

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
         
  has_one :membership
  has_many :forms
  
  
  groupify :named_group_member
  groupify :group
  after_save :create_default_membership
  
  def create_default_membership
    
  user = self.build_membership
  #user.membership_name = 'Member'
  user.admin = 1
  user.save
  end

end



@denniswoot denniswoot changed the title ActiveRecord::RecordInvalid: Validation failed: Group must exist from (irb):15 2.3.0 :016 > ActiveRecord::RecordInvalid: Validation failed: Group must exist Dec 13, 2018
@meatherly
Copy link

@pietjepuk111 I think this is because of the new ActiveRecord settings with Rails >5
you can fix this by adding self.belongs_to_required_by_default = false to your GroupMembership model.

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

No branches or pull requests

2 participants