2010-05-12 37 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