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

Cannot get single table inheritance work #54

Open
0000sir opened this issue Dec 29, 2016 · 4 comments
Open

Cannot get single table inheritance work #54

0000sir opened this issue Dec 29, 2016 · 4 comments
Assignees
Labels

Comments

@0000sir
Copy link

0000sir commented Dec 29, 2016

Thank you for your work, it a great gem. But I have some issue while using single table inheritance. Models are listed below:

class User < ActiveRecord::Base
  groupify :group_member, group_class_name: 'Department'
end

class Group < ActiveRecord::Base
  groupify :group
end

class GroupMembership < ActiveRecord::Base
  groupify :group_membership
end

class Department < Group
  has_ancestry
  has_members :users
  
  def add_employee(u)
    self.add u, as: :employee
  end
  
  def add_manager(u)
    self.add u, as: :manager
  end
  
  def remove_employee(u)
    u.groups.delete self
  end
  
  def remove_manager(u)
    u.groups.delete self
  end
  
end

I can add a user to a department as well, but when I trying to delete it from the department, it not works, and the sql is:

DELETE FROM "group_memberships" WHERE "group_memberships"."member_id" = ? AND "group_memberships"."member_type" = ? AND "group_memberships"."group_type" = 'Group' AND "group_memberships"."group_id" = 1 AND "group_memberships"."group_type" = ?  [["member_id", 1], ["member_type", "User"], ["group_type", "Group"]]

But the relationship generated in group_memberships, the group_type is Department, so nothing happened after the deletion sql.

This also happens while retrieving department members, department.users runs this sql:

SELECT DISTINCT "users".* FROM "users" INNER JOIN "group_memberships" ON "users"."id" = "group_memberships"."member_id" WHERE "group_memberships"."group_id" = ? AND "group_memberships"."group_type" = ? AND "group_memberships"."member_type" = 'User' AND "group_memberships"."membership_type" = ?  [["group_id", 1], ["group_type", "Group"], ["membership_type", :manager]]

It wont find anything because of wrong group_type, can you give more detail of using single table inheritance? It would be a big help, thank you

@dwbutler dwbutler added the bug label Dec 31, 2016
@dwbutler
Copy link
Owner

This sounds like a bug. I'll do some more investigation.

@dwbutler dwbutler self-assigned this Jan 5, 2017
@asafigan
Copy link

I was having trouble with this too.
Whenever I add a member to a class that inherited from Group, the group type on those member would all be Group not the class name that inherited from Group

@joelvh
Copy link
Collaborator

joelvh commented Jun 4, 2017

@0000sir @asafigan I don't seem to have the described problem deleting and selecting using the branch I'm working on to do some refactoring. Could you see if the bug is fixed for you?

Check out #61

@joelvh
Copy link
Collaborator

joelvh commented Aug 10, 2017

@0000sir @asafigan have you guys checked out #61 for single-table inheritance and polymoprhic improvements? This may become the 1.0 release of Groupify, so your feedback on this refactor is greatly appreciated. Thanks.

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

No branches or pull requests

4 participants