dwbutler / groupify

Add group and membership functionality to your Rails models
MIT License
194 stars 42 forks source link

User.in_named_group("some_string).count causes crash #42

Closed igostavro closed 8 years ago

igostavro commented 8 years ago

this code:

def create

    other_users = User.in_named_group( team_params[:team_name] )

    if other_users.count == 0 #<-- crash here
      redirect_to request.referrer, :flash => { :error => "Group Already Exists!" }
    end

end

Causes the following crash:

PG::UndefinedFunction at /teams
ERROR:  operator does not exist: integer = uuid
LINE 1: ...p_memberships" ON "group_memberships"."member_id" = "users"....
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Any ideas?

dwbutler commented 8 years ago

My best guess is that your schema defines "group_memberships"."member_id" as an integer (the default), and your users table defines its primary key as a uuid rather than an integer. This causes a type mismatch.

Please let me know if that's what's going on.

igostavro commented 8 years ago

thats it actually. What is the best course of action? Fix the migration for the group_memberships? If so can you provide code... new to ruby, sorry.

dwbutler commented 8 years ago

It kind of depends. the group_memberships table holds membership information for all models, not just users. So all models that you plan to put into a group would have to have the same column type. If you really need uuid, then group_memberships.member_id will also need to be of type uuid.

Another option you could explore is to make group_memberships.member_id of type string. Postgres might be able to coerce a uuid into a string. The performance could get pretty bad if you have lots of memberships inserted over time.

igostavro commented 8 years ago

ok i seemed to fix this with a migration:

class ChangeGroupMemberIdToUuid < ActiveRecord::Migration
  def up
    remove_column :group_memberships, :member_id
    add_column :group_memberships, :member_id, :uuid
  end

  def down
    remove_column :group_memberships, :metric_id
    add_column :group_memberships, :member_id, :integer
  end
end
dwbutler commented 8 years ago

Cool. You could also do:

class ChangeGroupMemberIdToUuid < ActiveRecord::Migration
  def change
    change_column :group_memberships, :member_id, :uuid
  end
end
igostavro commented 8 years ago

I actually tried that and got an error about not being able to coerce integer to uuid. Did you get a different result?

dwbutler commented 8 years ago

I see. I didn't try it. That error makes sense, since you can't convert an integer into a UUID. If your database is empty I would expect Postgres to just change the column and not complain about coercion.

igostavro commented 8 years ago

Ah, makes sense, maybe I should have nuked the table before running the migration ;-)

Thanks for your help!