{"action":"created","issue":{"url":"https://api.github.com/repos/AnalyticsMediaGroup/strongbolt/issues/26","repository_url":"https://api.github.com/repos/AnalyticsMediaGroup/strongbolt","labels_url":"https://api.github.com/repos/AnalyticsMediaGroup/strongbolt/issues/26/labels{/name}","comments_url":"https://api.github.com/repos/AnalyticsMediaGroup/strongbolt/issues/26/comments","events_url":"https://api.github.com/repos/AnalyticsMediaGroup/strongbolt/issues/26/events","html_url":"https://github.com/AnalyticsMediaGroup/strongbolt/issues/26","id":194895984,"number":26,"title":"HABTM Tables issue - ","user":{"login":"chriskwss","id":24517058,"avatar_url":"https://avatars.githubusercontent.com/u/24517058?v=3","gravatar_id":"","url":"https://api.github.com/users/chriskwss","html_url":"https://github.com/chriskwss","followers_url":"https://api.github.com/users/chriskwss/followers","following_url":"https://api.github.com/users/chriskwss/following{/other_user}","gists_url":"https://api.github.com/users/chriskwss/gists{/gist_id}","starred_url":"https://api.github.com/users/chriskwss/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chriskwss/subscriptions","organizations_url":"https://api.github.com/users/chriskwss/orgs","repos_url":"https://api.github.com/users/chriskwss/repos","events_url":"https://api.github.com/users/chriskwss/events{/privacy}","received_events_url":"https://api.github.com/users/chriskwss/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-12-12T05:53:18Z","updated_at":"2016-12-12T14:20:27Z","closed_at":null,"body":"Bolting an existing application with Tenant model. \r\n\r\nconfig.tenants = \"School\"\r\n\r\nStudents model is defined with\r\n```\r\nclass Student < ActiveRecord::Base\r\n\thas_and_belongs_to_many :users\r\n\thas_and_belongs_to_many :schools\r\n```\r\n\r\nIn strongbolt.rb I have the following configured: \r\n```\r\nconfig.models = 'User', 'School', 'Student',  'Subject',\r\n... \r\n'Student::HABTM_Users', 'School::HABTM_Students', 'School::HABTM_Users', 'Subject::HABTM_Users'\r\n```\r\nWhen attempting to add a new Student via \r\n\r\n```\r\n  def create\r\n    if current_user.can?(:create, Strongbolt::Role)\r\n      logger.debug \"**** Student::HABTM_Users.owned #{Student::HABTM_Users.owned?}\"\r\n      logger.debug \"**** Student::HABTM_Users.owner_attribute #{Student::HABTM_Users.owner_attribute}\"\r\n      logger.debug \"**** current_user.capabilities #{current_user.capabilities}\"\r\n      logger.debug \"**** current_user.can?(:create, Student::HABTM_Users) #{current_user.can?(:create, Student::HABTM_Users)}\"\r\n      @student = Student.new(student_params)\r\n<snip>\r\n```\r\n\r\nI get the following error: \r\n**Strongbolt::Unauthorized in StudentsController#create\r\ncreate permission not granted to User:1 for resource HABTM_Users:**\r\n\r\nBut the user I'm using should be able to access Student::HABTM_Users -- from the debug output: \r\n\r\n```\r\n**** Student::HABTM_Users.owned true\r\n**** Student::HABTM_Users.owner_attribute user_id\r\n**** current_user.capabilities [#<Strongbolt::Capability id: 1, name: nil, description: nil, model: \"AccRecord\", action: \"find\", attr: nil, require_ownership: false, require_tenant_access: false,\r\n<snip>\r\n#<Strongbolt::Capability id: 86, name: nil, description: nil, model: \"Student::HABTM_Users\", action: \"create\", attr: nil, require_ownership: false, require_tenant_access: false, created_at: \"2016-12-12 02:55:44\", updated_at: \"2016-12-12 02:55:44\">,\r\n<snip>\r\nAuthorizing user to perform create on Student::HABTM_Users(student_id: integer, user_id: integer)\r\n**** current_user.can?(:create, Student::HABTM_Users) false\r\n<snip>\r\nAuthorizing user to perform create on #<Student::HABTM_Users student_id: 33, user_id: 1>\r\nPopulating tenants cache for user 1\r\nAuthorizing user to perform find on School(id: integer, school_name: string, school_street: string, school_state: string, school_zip: string, school_phone: string, school_contact_name: string, created_at: datetime, updated_at: datetime, school_city: string)\r\n   (0.3ms)  SELECT `schools`.`id` FROM `schools`\r\n2 School\r\n   (1.1ms)  ROLLBACK\r\nCompleted 500 Internal Server Error in 57ms (ActiveRecord: 6.0ms)\r\n\r\nStrongbolt::Unauthorized (create permission not granted to User:1 for resource HABTM_Users:):\r\n```\r\n\r\nI see where there is \r\n**#<Strongbolt::Capability id: 86, name: nil, description: nil, model: \"Student::HABTM_Users\", action: \"create\", attr: nil, require_ownership: false, require_tenant_access: false, created_at: \"2016-12-12 02:55:44\", updated_at: \"2016-12-12 02:55:44\">**\r\n\r\nIt looks like any HABTM associations are being missed? "},"comment":{"url":"https://api.github.com/repos/AnalyticsMediaGroup/strongbolt/issues/comments/266442215","html_url":"https://github.com/AnalyticsMediaGroup/strongbolt/issues/26#issuecomment-266442215","issue_url":"https://api.github.com/repos/AnalyticsMediaGroup/strongbolt/issues/26","id":266442215,"user":{"login":"tobithiel","id":956860,"avatar_url":"https://avatars.githubusercontent.com/u/956860?v=3","gravatar_id":"","url":"https://api.github.com/users/tobithiel","html_url":"https://github.com/tobithiel","followers_url":"https://api.github.com/users/tobithiel/followers","following_url":"https://api.github.com/users/tobithiel/following{/other_user}","gists_url":"https://api.github.com/users/tobithiel/gists{/gist_id}","starred_url":"https://api.github.com/users/tobithiel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tobithiel/subscriptions","organizations_url":"https://api.github.com/users/tobithiel/orgs","repos_url":"https://api.github.com/users/tobithiel/repos","events_url":"https://api.github.com/users/tobithiel/events{/privacy}","received_events_url":"https://api.github.com/users/tobithiel/received_events","type":"User","site_admin":false},"created_at":"2016-12-12T14:20:27Z","updated_at":"2016-12-12T14:20:27Z","body":"Yeah, `has_and_belongs_to_many` associations don't really work right now. They are a bit difficult to work with, because there is no explicit join model class. ActiveRecord creates one for each side individually (so you end up with two) and you usually have to grant access to both. That's why we started preferring `has_many ..., through: ...`, where the join model class is explicit.\r\n\r\nThe bug you're experiencing is due to the fact, that the implicit join model class strips the namespace from its name. It should be `Student::HABTM_Users` and not `HABTM_Users`. You can see this by calling `Student::HABTM_Users.name => \"HABTM_Users\"`, whereas normally any class would return `Student::HABTM_Users.name => \"Student::HABTM_Users\"`.\r\n\r\nMy quick & dirty workaround to get it working was to do something like this:\r\n```ruby\r\nclass Student::HABTM_Users\r\n  def self.name\r\n    \"Student::HABTM_Users\"\r\n  end\r\nend\r\n```\r\n\r\nLet me know if that helps in your situation as well.\r\nThat's obviously not a long-term solution, but I'm not sure yet if Strongbolt should work around this edge case or if we should open an issue with ActiveRecord, that they should return the correct name (Technically these implicit join model classes are private and shouldn't be accessed from the outside as far as I am aware)."}}
{"actor":{"display_login":"tobithiel"}}