Open jeremen opened 8 years ago
Have you included the resourcify
macro in the resource model?
See https://github.com/RolifyCommunity/rolify#32-configure-your-resource-models
Rolify hasn't been tested against Rails 5 in any way as to my knowledge.
I have exactly same problem.
I tried this:
2.3.0 :017 > r = Role.new(name: 'admin') 2.3.0 :018 > r.valid? => false 2.3.0 :023 > r.errors.messages => {:resource=>["must exist"]}
Rails 5.0.0.beta2 Rolify 5.0.0 Pg 0.18.4
Any ideas?
@rubydev As mentioned above, Rolify hasn't been tested against Rails 5, we're awaiting the release of it and will evaluate which versions to support (leaning towards 4 and 5 personally, though if 3 isn't too much of a challenge to support that works.)
Rails 5 has new option:
Rails.application.config.active_record.belongs_to_required_by_default = true
so you must switch this to false or modify code in role model:
belongs_to :resource, polymorphic: true, optional: true
(added "optional: true")
rails 5.0.2 ruby 2.3.2 rolify 5.10
Even with above options (by rubydev) habtm relation is not creating, instead there is "weird" queries for select and role update (unlike in rails 4, rolify 4):
chatapp(dev)> room = Room.find 12
chatapp(dev)> user = User.find 2
chatapp(dev)> user.add_role :moder, room
Role Load (0.8ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = $1 AND "roles"."resource_type" = $2 AND "roles"."resource_id" = $3 ORDER BY "roles"."id" ASC LIMIT $4 [["name", "moder"], ["resource_type", "Room"], ["resource_id", 12], ["LIMIT", 1]]
(0.2ms) BEGIN
Room Load (0.3ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = $1 LIMIT $2 [["id", 12], ["LIMIT", 1]]
SQL (1.0ms) INSERT INTO "roles" ("name", "resource_type", "resource_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "moder"], ["resource_type", "Room"], ["resource_id", 12], ["created_at", 2017-04-27 09:44:21 UTC], ["updated_at", 2017-04-27 09:44:21 UTC]]
(0.7ms) COMMIT
Role Exists (1.2ms) SELECT 1 AS one FROM "roles" WHERE "roles"."resource_id" = $1 AND "roles"."resource_type" = $2 AND "roles"."id" = $3 LIMIT $4 [["resource_id", 2], ["resource_type", "User"], ["id", 8], ["LIMIT", 1]]
Role Exists (0.5ms) SELECT 1 AS one FROM "roles" WHERE "roles"."name" = $1 LIMIT $2 [["name", :moder], ["LIMIT", 1]]
Role Exists (0.6ms) SELECT 1 AS one FROM "roles" WHERE "roles"."name" = $1 AND "roles"."resource_type" = $2 AND "roles"."resource_id" = $3 LIMIT $4 [["name", :moder], ["resource_type", "Room"], ["resource_id", 12], ["LIMIT", 1]]
(0.4ms) SELECT "roles".id FROM "roles" WHERE "roles"."resource_id" = $1 AND "roles"."resource_type" = $2 [["resource_id", 2], ["resource_type", "User"]]
Role Load (0.3ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = 8
Role Load (0.2ms) SELECT "roles".* FROM "roles" WHERE "roles"."resource_id" = $1 AND "roles"."resource_type" = $2 [["resource_id", 2], ["resource_type", "User"]]
(0.1ms) BEGIN
User Load (3.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
SQL (0.7ms) UPDATE "roles" SET "resource_id" = $1, "resource_type" = $2, "updated_at" = $3 WHERE "roles"."id" = $4 [["resource_id", 2], ["resource_type", "User"], ["updated_at", 2017-04-27 09:44:21 UTC], ["id", 8]]
(0.4ms) COMMIT
=> #<Role id: 8, name: "moder", roomesource_type: "Room", roomesource_id: 12, created_at: "2017-04-27 09:44:21", updated_at: "2017-04-27 09:44:21">
chatapp(dev)> user.has_role? :moder
Role Load (0.5ms) SELECT "roles".* FROM "roles" WHERE "roles"."resource_id" = $1 AND "roles"."resource_type" = $2 AND (((roles.name = 'moder') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["resource_id", 2], ["resource_type", "User"]]
=> false
chatapp(dev)> Role.last
Role Load (0.2ms) SELECT "roles".* FROM "roles" ORDER BY "roles"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Role id: 8, name: "moder", roomesource_type: "User", roomesource_id: 2, created_at: "2017-04-27 09:44:21", updated_at: "2017-04-27 09:44:21">
chatapp(dev)> user.has_role? :moder, room
Role Load (0.6ms) SELECT "roles".* FROM "roles" WHERE "roles"."resource_id" = $1 AND "roles"."resource_type" = $2 AND ((((roles.name = 'moder') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'moder') AND (roles.resource_type = 'Room') AND (roles.resource_id IS NULL)) OR ((roles.name = 'moder') AND (roles.resource_type = 'Room') AND (roles.resource_id = 12)))) [["resource_id", 2], ["resource_type", "User"]]
=> false
chatapp(dev)>
this happen because i had resourcify
in User model :)
Guys, I'm with the same problem.
user.add_role :teacher
Role Load (0.2ms) SELECT "roles".* FROM "roles" WHERE "roles"."name" = ? AND "roles"."resource_type" IS NULL AND "roles"."resource_id" IS NULL ORDER BY "roles"."id" ASC LIMIT ? [["name", "teacher"], ["LIMIT", 1]]
(0.2ms) begin transaction
(0.1ms) rollback transaction
HABTM_Roles Load (0.1ms) SELECT "users_roles".* FROM "users_roles" WHERE "users_roles"."user_id" = ? [["user_id", 1]]
Same here.
I'm seeing
ActiveRecord::AssociationTypeMismatch: Role(#61166240) expected, got NilClass(#18472880)
With Rails 5, rolify 5.1, mysql2 0.4.5
Still happening, throwing: ["Resource must exist"], optional: true added to belongs_to :resource, :polymorphic => true
@quevon24 @RowanMcDonald @murilomendes (i know it is late, but maybe this can help oother dev's users) This is happening maybe because you don't have "rolify" in your User model... instead you have "resourcify". So remove resourcify (resourcify just link any resource_type and resource_id ability with Role model) and add rolify in your user model. I get it works, but unfortunately i get an error about undefined method rolify when add_role for resource_type and resource_id linked, not for a simple role name (string or tag anyway... look, at the source code, it does convert the argument to string).
rolify 5 rails 5 sqlite3
Moreover addition of roles through Role model doesn't work as well