プロジェクトの 1 つのモデルを次のように設定しています (Rails 3.2 と Mongoid 3.0 を使用):
class Parent
include Mongoid::Document
has_many :kids
def schools
return kids.map { |kid| kid.school }
end
end
class School
include Mongoid::Document
has_many :kids
end
class Kid
include Mongoid::Document
belongs_to :parent
belongs_to :school
end
親モデルは、Devise でセットアップした標準のユーザー モデルとして機能します。SchoolController
親が子供がいる学校へのアクセスのみを許可するindex
andメソッドが必要show
です。このサイトによると、これを行う最良の方法: http://www.therailsway.com/2007/3/26/association -proxies-are-your-friend/は、次のようなことを行います:
def index
@schools = current_user.schools
end
def show
@school = current_user.schools.find(params[:id])
end
ただし、Mongoid はhas_many :through
リレーションを許可しないためParent#schools
、連想プロキシではなく配列を返すカスタム メソッドであるため、#find
使用できるメソッドではありません。ドキュメントの配列から関連付けプロキシを作成する方法はありますか? または、この単純なアクセス制御の問題を処理するよりスマートな方法はありますか?