ruby-on-railsruby-on-rails-3authorizationcancanhas-and-belongs-to-many

How can i setup load_and_authorize_resource through a habtm association?


I have tasks which I want to display in project context. They are associated through a habtm association. So I have three tables: projects, tasks and projects_tasks.

resources :projects do
  resources :tasks
end

class Project < ActiveRecord::Base
  has_and_belongs_to_many :tasks
  ...

class Task < ActiveRecord::Base
  has_and_belongs_to_many :projects
  ...

How can i setup load_and_authorize_resource through a habtm association?

# ???
class TasksController < ApplicationController
  load_and_authorize_resource :project
  load_and_authorize_resource :task, :through => :project, :shallow => true
  ...

Solution

  • You might want to make you join table a model such that a Project has_many :tasks, :through => : assignments