2010-05-12 74 views
0

我有一個應用程序,它具有Basecamp風格的子域名,也就是我有項目,用戶,蘋果和橘子。用戶,蘋果和桔子全部用於項目,只存在於http://project.myapp.com。我向用戶,蘋果和橙子添加了一個project_id,並且一切正常,除了這三個對象的ID在全局增加,並且在我的應用程序中,我通過該ID查找對象。基地陣營風格的子域和模型ID

這似乎不是最佳實踐。我應該用輔助鍵進行查找嗎?這如何影響效率?如果有一篇很好的博客文章涵蓋了這一點,那就太棒了。

回答

2

可以有一個全局ID(在數據庫中)。如果可能,請不要顯示這些數據庫ID,而應使用友好的URL。

無論如何,你不應該信任你的用戶:即使你有ID,檢查記錄是否與項目相關聯。

2

在你的控制器只是範圍的一切項目,假設項目has_many :apples

class ApplesController < ApplicationController 
    before_filter :find_apple 

    private 
    def find_apple 
     if current_user.is_admin? 
     @apple = Apple.find(params[:id]) 
     else 
     # Scope to the current project/subdomain 
     # Note the use of current_project 
     # You need to exchange this with whatever you use to get the project object 
     @apple = current_project.apples.find(params[:id]) 

     # Do something here if @apple is nil, like redirect 
     end 
    end 
end