如何确定“多态控制器”的范围?到rails3多租户子域样式应用程序中的current_account

发布于 2024-11-02 23:25:46 字数 1551 浏览 1 评论 0原文

我正在尝试在帖子模型、上传模型等上使用多态注释模型。通常我会有一个 @parent 资源来将一个资源范围扩展到另一个资源,以便 Rails 建立关系。但因为这是一个多租户子域样式的应用程序,其中所有资源也需要限定在 current_account 范围内。我正在努力解决如何在 current_accout 下确定 @parent 资源的范围。

我有一个 current_account 方法、一个 find_parent 和一个 Parent_collection 方法

#Application_controller
class ApplicationController < ActionController::Base
  before_filter :current_account

  def current_account
    unless is_root_domain?
     @current_account ||= Account.find_by_subdomain(request.subdomains.first)
    end
  @current_account
  end

 def find_parent
   params.each do |name ,value|
    @parent = $1.pluralize.classify.constantize.find(value) if name =~ /(.*?)_id/
  return if @parent
  end
 end

 def parent_collection
   @parent_collection ||= current_account.send parent.pluralize
 end

#comments_controller with only @parent resource without reference to current_account
class CommentsController < ApplicationController
  before_filter :find_parent

  def new
    @comment = @parent.comments.build
  end

  def create
   @comment = @parent.comments.build(params[:comment])
   .....
   .....
  end
end

#comments_controller using only current_account resource without reference to @parent
class CommentsController < ApplicationController
  before_filter :current_account

  def new
    @comment = current_account.comments.build
  end

  def create
    @comment = current_account.comments.build(params[:comment])
    .....
    .....
  end
end

在 ApplicationController 中, 应用程序_控制器。谢谢

I am trying to use a polymorphic comment model on the post model , upload model, etc. Ordinarily i would have a @parent resource to scope one to the other in order for Rails to build the relationship. But because this a multi-tenant subdomain styled application, where all resources will also need to be scoped to the curent_account. I am struggling with how to scope @parent resource under the current_accout.

In ApplicationController I have a current_account method, a find_parent and a parent_collection method:

#Application_controller
class ApplicationController < ActionController::Base
  before_filter :current_account

  def current_account
    unless is_root_domain?
     @current_account ||= Account.find_by_subdomain(request.subdomains.first)
    end
  @current_account
  end

 def find_parent
   params.each do |name ,value|
    @parent = $1.pluralize.classify.constantize.find(value) if name =~ /(.*?)_id/
  return if @parent
  end
 end

 def parent_collection
   @parent_collection ||= current_account.send parent.pluralize
 end

end

#comments_controller with only @parent resource without reference to current_account
class CommentsController < ApplicationController
  before_filter :find_parent

  def new
    @comment = @parent.comments.build
  end

  def create
   @comment = @parent.comments.build(params[:comment])
   .....
   .....
  end
end

#comments_controller using only current_account resource without reference to @parent
class CommentsController < ApplicationController
  before_filter :current_account

  def new
    @comment = current_account.comments.build
  end

  def create
    @comment = current_account.comments.build(params[:comment])
    .....
    .....
  end
end

Any guide on how to call current_accout in the controllers in a way that @parent is scoped to it and is there a need for the parent_collection method that i put in the applications_controller. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2024-11-09 23:25:46

让我们假设您的 @parent 资源是一个 Post 模型。然后我会想象:
a) 您的 Account 模型 has_many :posts
b) 您的 Post 模型 belongs_to :account
c) 您的 Post 模型 has_many :comments, :as => :可评论
c) 您的 Comment 模型 belongs_to :commentable, :polymorphic => true

因此,考虑到这一点,您应该能够构建这样的范围:

@parent.where(:account_id => current_account.id).comments

您还可以将其重构为可注释模型:

def Post < ActiveRecord::Base
  scope :by_account, lambda { |account_id| where(:account_id => account_id) }
end

并在控制器中使用它,如下所示:

@parent.by_account(current_account.id).comments

Let's make an assumption that your @parent resource is a Post model. Then I would imagine that:
a) your Account model has_many :posts
b) your Post model belongs_to :account
c) your Post model has_many :comments, :as => :commentable
c) your Comment model belongs_to :commentable, :polymorphic => true

So with that in mind you should be able to build scopes like this:

@parent.where(:account_id => current_account.id).comments

You can also refactor it to the commentable model:

def Post < ActiveRecord::Base
  scope :by_account, lambda { |account_id| where(:account_id => account_id) }
end

and the use it in the controller like this:

@parent.by_account(current_account.id).comments
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文