Rails: best practice to scope queries based on subdomain?

后端 未结 5 1498
傲寒
傲寒 2020-12-24 04:17

I\'m working on a Rails (currently 2.3.4) app that makes use of subdomains to isolate independent account sites. To be clear, what I mean is foo.mysite.com should show the f

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-24 04:39

    Be careful going with default scope here, as it will lead you into a false sense of security, particularly when creating records.

    I've always used your first example to keep this clear:

    @page = @go.pages.find(params[:id])
    

    The biggest reason is because you also want to ensure this association is applied to new records, so your new/create actions will look like the following, ensuring that they are properly scoped to the parent association:

    # New
    @page = @go.pages.new
    
    # Create
    @page = @go.pages.create(params[:page])
    

提交回复
热议问题