我有以下型号:
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
我希望能够仅显示用户有权访问的任务(即,在表read
中设置了标志)。我可以使用以下查询来完成此操作,但对我来说这似乎不是 Rails-y:true
Permissions
@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])
有人知道这样做的正确方法吗?