1

私は承認のためにacl9rails3を使用しています。私はARelとレールのクエリ方法を理解しようとしています。

Companyに属するUserがあり、そのユーザーはCompanyに対して特定の役割 (acl9 が配管を提供します) を持っています。特定の役割を持つ特定の会社からすべてのユーザーを取得するにはどうすればよいですか? アプリケーションで結果をフィルタリングするのではなく、 DB で結果をフィルタリングしたい。

私はこのようなものが欲しい:

# 
# authorizable_id = 1 is the company's id
# 'collaborator' and '1' would be params for my scope
#
select * from users
inner join roles_users
on roles_users.user_id = users.id
inner join roles
on roles_users.role_id = roles.id
where roles.authorizable_type='Company'
and   roles.authorizable_id = 1
and   roles.name = 'collaborator';

#usage with scope
@collaborators = User.with_role_and_company('collaborator',current_user.company)

rails3 には sql api があることは知っていますが、私は Java/Grails の出身であり、使用する ORM のほとんどにはいくつかの ORM api があります (datamapper はこのように機能すると思いますが、私はプレーンな rails3 を使用しています)。

4

2 に答える 2

1

私が見つけた:

 class << self
   def with_role_in_company(role, company)
      joins(:roles).
          where(:roles => {:authorizable_type => 'Company'}).
          where(:roles => {:authorizable_id => company.id}).
          where(:roles => {:name => role})
    end
  end
于 2010-11-23T00:14:57.063 に答える