這是您要使用(http://wiki.rubyonrails.org/howtos/db-relationships/polymorphic)
多態關聯您的作業模式的東西,你會做以下幾點:
has_many :permissions, :as => :subject
而在你的權限模型,你可以這樣做:
belongs_to :subject, :polymorphic => true
但是,您需要將subject_class字段重命名爲subject_type才能隱式地工作。當所有這些都完成後,您將擁有從Rails免費索取的所有內容。創建新權限時,您不需要傳遞:subject_type。您可以創建一個像這樣的權限和:subject_type將自動爲您填充:
assignment = Assignment.create(:title => "My Assignment")
permission = Permission.create(:subject => assignment, :action => "read", :user => current_user)
- or -
assignment.permission.create(:user => current_user, :action => "read")