I have the following models:
class User < ActiveRecord::Base
has_many :permissions
has_many :tasks, :through => :permissions
class Task < ActiveRecord::Base
has_many :permissions
has_many :users, :through => :permissions
class Permission < ActiveRecord::Base
belongs_to :task
belongs_to :user
I want to be able to display only tasks which a user has access to (i.e., the read
flag is set to true
in the Permissions
table). I can accomplish this with the following query, but it doesn't seem very Rails-y to me:
@user = current_user
@tasks = @user.tasks.find_by_sql(["SELECT * FROM tasks INNER JOIN permissions ON tasks.id = permissions.task_id WHERE permissions.read = true AND permissions.user_id = ?", @user.id])
Anyone know the right way to do this?